Skip to content

Commit 2723a23

Browse files
committed
Isolation levels not being reset on error. Fixes #469. Thanks @anthony
1 parent c662152 commit 2723a23

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v4.2.12
2+
3+
#### Fixed
4+
5+
* Isolation levels not being reset on error. Fixes #469. Thanks @anthony
6+
7+
18
## v4.2.11
29

310
#### Fixed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.2.11
1+
4.2.12

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def begin_isolated_db_transaction(isolation)
5858

5959
def set_transaction_isolation_level(isolation_level)
6060
do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}"
61-
begin_db_transaction
6261
end
6362

6463
def commit_db_transaction

lib/active_record/connection_adapters/sqlserver/transaction.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def commit
3636
reset_starting_isolation_level
3737
end
3838

39+
def rollback
40+
super
41+
reset_starting_isolation_level
42+
end
43+
3944
private
4045

4146
def reset_starting_isolation_level

test/cases/transaction_test_sqlserver.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ class TransactionTestSQLServer < ActiveRecord::TestCase
3232
end
3333

3434
it 'can use an isolation level and reverts back to starting isolation level' do
35-
begin
36-
in_level = nil
37-
begin_level = connection.user_options_isolation_level
38-
begin_level.must_match %r{read committed}i
39-
Ship.transaction(isolation: :serializable) do
40-
Ship.create! name: 'Black Pearl'
41-
in_level = connection.user_options_isolation_level
42-
end
43-
after_level = connection.user_options_isolation_level
44-
in_level.must_match %r{serializable}i
45-
after_level.must_match %r{read committed}i
46-
ensure
47-
connection.set_transaction_isolation_level 'READ COMMITTED'
35+
in_level = nil
36+
begin_level = connection.user_options_isolation_level
37+
begin_level.must_match %r{read committed}i
38+
Ship.transaction(isolation: :serializable) do
39+
Ship.create! name: 'Black Pearl'
40+
in_level = connection.user_options_isolation_level
4841
end
42+
after_level = connection.user_options_isolation_level
43+
in_level.must_match %r{serializable}i
44+
after_level.must_match %r{read committed}i
45+
end
46+
47+
it 'can use an isolation level and reverts back to starting isolation level under exceptions' do
48+
connection.user_options_isolation_level.must_match %r{read committed}i
49+
lambda {
50+
Ship.transaction(isolation: :serializable) { Ship.create! }
51+
}.must_raise(ActiveRecord::RecordInvalid)
52+
connection.user_options_isolation_level.must_match %r{read committed}i
4953
end
5054

5155

0 commit comments

Comments
 (0)