提交 01adc456 编写于 作者: D Dylan Thacker-Smith

activerecord/mysql2: Avoid setting @connection to nil, just close it

By doing `@connection = nil` that means that we need nil checks before it
is used anywhere, but we weren't doing those checks.  Instead, we get a
NoMethodError after using a connection after it fails to reconnect.

Neither of the other adapters set @connection to nil, just the mysql2
adapter. By just closing it, we avoid the need to check if we have a
connection object and it will produce an appropriate exception when used.
上级 087427e0
......@@ -24,11 +24,9 @@ def select_rows(sql, name = nil, binds = [])
# Executes the SQL statement in the context of this connection.
def execute(sql, name = nil)
if @connection
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
end
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
super
end
......@@ -71,11 +69,9 @@ def select_result(sql, name = nil, binds = [])
end
def exec_stmt_and_free(sql, name, binds, cache_stmt: false)
if @connection
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
end
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
type_casted_binds = type_casted_binds(binds)
......
......@@ -90,7 +90,6 @@ def quote_string(string)
#++
def active?
return false unless @connection
@connection.ping
end
......@@ -105,10 +104,7 @@ def reconnect!
# Otherwise, this method does nothing.
def disconnect!
super
unless @connection.nil?
@connection.close
@connection = nil
end
@connection.close
end
private
......
......@@ -63,6 +63,27 @@ def test_successful_reconnection_after_timeout_with_verify
assert @connection.active?
end
def test_execute_after_disconnect
@connection.disconnect!
error = assert_raise(ActiveRecord::StatementInvalid) do
@connection.execute("SELECT 1")
end
assert_match /closed MySQL connection/, error.message
end
def test_quote_after_disconnect
@connection.disconnect!
error = assert_raise(Mysql2::Error) do
@connection.quote("string")
end
assert_match /closed MySQL connection/, error.message
end
def test_active_after_disconnect
@connection.disconnect!
assert_equal false, @connection.active?
end
def test_mysql_connection_collation_is_configured
assert_equal "utf8_unicode_ci", @connection.show_variable("collation_connection")
assert_equal "utf8_general_ci", ARUnit2Model.connection.show_variable("collation_connection")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册