Skip to content

Commit

Permalink
Ensure disconnecting or reconnecting resets the transaction state
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Sep 14, 2012
1 parent 6195142 commit 02f5655
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -74,6 +74,7 @@ def active?
end

def reconnect!
super
disconnect!
connect
end
Expand All @@ -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
Expand Down
Expand Up @@ -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

Expand Down
Expand Up @@ -431,7 +431,7 @@ def active?

# Close then reopen the connection.
def reconnect!
clear_cache!
super
@connection.reset
configure_connection
end
Expand All @@ -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

Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Expand Up @@ -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
32 changes: 32 additions & 0 deletions activerecord/test/cases/adapter_test.rb
Expand Up @@ -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

0 comments on commit 02f5655

Please sign in to comment.