Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ def begin_db_transaction
end

def commit_db_transaction
do_execute "COMMIT TRANSACTION"
disable_auto_reconnect { do_execute "COMMIT TRANSACTION" }
end

def rollback_db_transaction
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
end

def create_savepoint
do_execute "SAVE TRANSACTION #{current_savepoint_name}"
disable_auto_reconnect { do_execute "SAVE TRANSACTION #{current_savepoint_name}" }
end

def release_savepoint
end

def rollback_to_savepoint
do_execute "ROLLBACK TRANSACTION #{current_savepoint_name}"
disable_auto_reconnect { do_execute "ROLLBACK TRANSACTION #{current_savepoint_name}" }
end

def add_limit_offset!(sql, options)
Expand Down
7 changes: 7 additions & 0 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,13 @@ def with_auto_reconnect
end
end

def disable_auto_reconnect
old_auto_connect, self.class.auto_connect = self.class.auto_connect, false
yield
ensure
self.class.auto_connect = old_auto_connect
end

def auto_reconnected?
return false unless auto_connect
@auto_connecting = true
Expand Down
39 changes: 39 additions & 0 deletions test/cases/connection_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,45 @@ def setup
end
end

context 'testing #disable_auto_reconnect' do
should 'when auto reconnect setting is on' do
with_auto_connect(true) do
@connection.send(:disable_auto_reconnect) do
assert !@connection.class.auto_connect
end
assert @connection.class.auto_connect
end
end

should 'when auto reconnect setting is off' do
with_auto_connect(false) do
@connection.send(:disable_auto_reconnect) do
assert !@connection.class.auto_connect
end
assert !@connection.class.auto_connect
end
end
end

should 'not auto reconnect on commit transaction' do
@connection.disconnect!
assert_raise(ActiveRecord::LostConnection) { @connection.commit_db_transaction }
end

should 'gracefully ignore lost connections on rollback transaction' do
@connection.disconnect!
assert_nothing_raised { @connection.rollback_db_transaction }
end

should 'not auto reconnect on create savepoint' do
@connection.disconnect!
assert_raise(ActiveRecord::LostConnection) { @connection.create_savepoint }
end

should 'not auto reconnect on rollback to savepoint ' do
@connection.disconnect!
assert_raise(ActiveRecord::LostConnection) { @connection.rollback_to_savepoint }
end
end


Expand Down