diff options
author | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2016-09-08 10:38:58 -0400 |
---|---|---|
committer | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2016-09-08 11:16:04 -0400 |
commit | 01adc45672672459c98880babb39ff5b357e1296 (patch) | |
tree | 31b2553b9664e1d7604659ef769b1782bd23e6fb /activerecord/lib/active_record | |
parent | 087427e0d1f7baf804ae52e13dc55bfa78a50123 (diff) | |
download | rails-01adc45672672459c98880babb39ff5b357e1296.tar.gz rails-01adc45672672459c98880babb39ff5b357e1296.tar.bz2 rails-01adc45672672459c98880babb39ff5b357e1296.zip |
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.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb | 16 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb | 6 |
2 files changed, 7 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb index c8238eb266..56800f7590 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb @@ -24,11 +24,9 @@ module ActiveRecord # 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 @@ module ActiveRecord 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) diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 0130b4ef62..a3e2c913c5 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -90,7 +90,6 @@ module ActiveRecord #++ def active? - return false unless @connection @connection.ping end @@ -105,10 +104,7 @@ module ActiveRecord # Otherwise, this method does nothing. def disconnect! super - unless @connection.nil? - @connection.close - @connection = nil - end + @connection.close end private |