Fixed that Active Record would throw Broken Pipe errors with FCGI when the...

Fixed that Active Record would throw Broken Pipe errors with FCGI when the MySQL connection timed out instead of reconnecting #428 [Nicholas Seckar]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@729 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 16774048
*SVN* *SVN*
* Fixed that Active Record would throw Broken Pipe errors with FCGI when the MySQL connection timed out instead of reconnecting #428 [Nicholas Seckar]
* Added options to specify an SSL connection for MySQL. Define the following attributes in the connection config (config/database.yml in Rails) to use it: sslkey, sslcert, sslca, sslcapath, sslcipher. To use SSL with no client certs, just set :sslca = '/dev/null'. http://dev.mysql.com/doc/mysql/en/secure-connections.html #604 [daniel@nightrunner.com] * Added options to specify an SSL connection for MySQL. Define the following attributes in the connection config (config/database.yml in Rails) to use it: sslkey, sslcert, sslca, sslcapath, sslcipher. To use SSL with no client certs, just set :sslca = '/dev/null'. http://dev.mysql.com/doc/mysql/en/secure-connections.html #604 [daniel@nightrunner.com]
* Added automatic dropping/creating of test tables for running the unit tests on all databases #587 [adelle@bullet.net.au] * Added automatic dropping/creating of test tables for running the unit tests on all databases #587 [adelle@bullet.net.au]
......
...@@ -36,12 +36,24 @@ def self.mysql_connection(config) # :nodoc: ...@@ -36,12 +36,24 @@ def self.mysql_connection(config) # :nodoc:
mysql = Mysql.init mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey] mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey]
ConnectionAdapters::MysqlAdapter.new(mysql.real_connect(host, username, password, database, port, socket), logger) ConnectionAdapters::MysqlAdapter.new(mysql.real_connect(host, username, password, database, port, socket), logger, [host, username, password, database, port, socket])
end end
end end
module ConnectionAdapters module ConnectionAdapters
class MysqlAdapter < AbstractAdapter # :nodoc: class MysqlAdapter < AbstractAdapter # :nodoc:
LOST_CONNECTION_ERROR_MESSAGES = [
"Server shutdown in progress",
"Broken pipe",
"Lost connection to MySQL server during query",
"MySQL server has gone away"
]
def initialize(connection, logger, connection_options=nil)
super(connection, logger)
@connection_options = connection_options
end
def select_all(sql, name = nil) def select_all(sql, name = nil)
select(sql, name) select(sql, name)
end end
...@@ -67,7 +79,17 @@ def insert(sql, name = nil, pk = nil, id_value = nil) ...@@ -67,7 +79,17 @@ def insert(sql, name = nil, pk = nil, id_value = nil)
end end
def execute(sql, name = nil) def execute(sql, name = nil)
log(sql, name, @connection) { |connection| connection.query(sql) } begin
return log(sql, name, @connection) { |connection| connection.query(sql) }
rescue ActiveRecord::StatementInvalid => exception
if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message.split(":").first =~ /^#{msg}/ }
@connection.real_connect(*@connection_options)
@logger.info("Retrying invalid statement with reopened connection") if @logger
return log(sql, name, @connection) { |connection| connection.query(sql) }
else
raise
end
end
end end
def update(sql, name = nil) def update(sql, name = nil)
...@@ -135,7 +157,8 @@ def quote_string(s) ...@@ -135,7 +157,8 @@ def quote_string(s)
private private
def select(sql, name = nil) def select(sql, name = nil)
result = nil result = nil
log(sql, name, @connection) { |connection| connection.query_with_result = true; result = connection.query(sql) } @connection.query_with_result = true
result = execute(sql, name)
rows = [] rows = []
all_fields_initialized = result.fetch_fields.inject({}) { |all_fields, f| all_fields[f.name] = nil; all_fields } all_fields_initialized = result.fetch_fields.inject({}) { |all_fields, f| all_fields[f.name] = nil; all_fields }
result.each_hash { |row| rows << all_fields_initialized.dup.update(row) } result.each_hash { |row| rows << all_fields_initialized.dup.update(row) }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册