Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure disconnecting or reconnecting resets the transaction state

  • Loading branch information...
commit 02f56554d68cddae02ccc4a8511cc5c64210d258 1 parent 6195142
@jonleighton jonleighton authored
View
11 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -69,7 +69,6 @@ class AbstractAdapter
def initialize(connection, logger = nil, pool = nil) #:nodoc:
super()
- @active = nil
@connection = connection
@in_use = false
@instrumenter = ActiveSupport::Notifications.instrumenter
@@ -187,19 +186,21 @@ def disable_referential_integrity
# checking whether the database is actually capable of responding, i.e. whether
# the connection isn't stale.
def active?
- @active != false
end
# Disconnects from the database if already connected, and establishes a
- # new connection with the database.
+ # new connection with the database. Implementors should call super if they
+ # override the default implementation.
def reconnect!
- @active = true
+ clear_cache!
+ reset_transaction
end
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
- @active = false
+ clear_cache!
+ reset_transaction
end
# Reset the state of this connection, directing the DBMS to clear
View
2  activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -74,6 +74,7 @@ def active?
end
def reconnect!
+ super
disconnect!
connect
end
@@ -82,6 +83,7 @@ def reconnect!
# Disconnects from the database if already connected.
# Otherwise, this method does nothing.
def disconnect!
+ super
unless @connection.nil?
@connection.close
@connection = nil
View
3  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -189,14 +189,15 @@ def active?
end
def reconnect!
+ super
disconnect!
- clear_cache!
connect
end
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
+ super
@connection.close rescue nil
end
View
4 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -431,7 +431,7 @@ def active?
# Close then reopen the connection.
def reconnect!
- clear_cache!
+ super
@connection.reset
configure_connection
end
@@ -444,7 +444,7 @@ def reset!
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
- clear_cache!
+ super
@connection.close rescue nil
end
View
8 activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -104,6 +104,8 @@ class BindSubstitution < Arel::Visitors::SQLite # :nodoc:
def initialize(connection, logger, config)
super(connection, logger)
+
+ @active = nil
@statements = StatementPool.new(@connection,
config.fetch(:statement_limit) { 1000 })
@config = config
@@ -154,11 +156,15 @@ def supports_add_column?
true
end
+ def active?
+ @active != false
+ end
+
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
super
- clear_cache!
+ @active = false
@connection.close rescue nil
end
View
4 activerecord/test/active_record/connection_adapters/fake_adapter.rb
@@ -36,6 +36,10 @@ def merge_column(table_name, name, sql_type = nil, options = {})
def columns(table_name)
@columns[table_name]
end
+
+ def active?
+ true
+ end
end
end
end
View
32 activerecord/test/cases/adapter_test.rb
@@ -160,4 +160,36 @@ def test_disable_referential_integrity
end
end
end
+
+ class AdapterTestWithoutTransaction < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @klass = Class.new(ActiveRecord::Base)
+ @klass.establish_connection 'arunit'
+ @connection = @klass.connection
+ end
+
+ def teardown
+ @klass.remove_connection
+ end
+
+ test "transaction state is reset after a reconnect" do
+ skip "in-memory db doesn't allow reconnect" if in_memory_db?
+
+ @connection.begin_transaction
+ assert @connection.transaction_open?
+ @connection.reconnect!
+ assert !@connection.transaction_open?
+ end
+
+ test "transaction state is reset after a disconnect" do
+ skip "in-memory db doesn't allow disconnect" if in_memory_db?
+
+ @connection.begin_transaction
+ assert @connection.transaction_open?
+ @connection.disconnect!
+ assert !@connection.transaction_open?
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.