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
10 changes: 9 additions & 1 deletion lib/active_record/connection_adapters/sqlserver/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ def sqlserver?
def current_isolation_level
return unless sqlserver?
level = connection.user_options_isolation_level
level.blank? ? 'READ COMMITTED' : level.upcase

# When READ_COMMITTED_SNAPSHOT is set to ON,
# user_options_isolation_level will be equal to 'read committed
# snapshot' which is not a valid isolation level
if level.blank? || level == 'read committed snapshot'
'READ COMMITTED'
else
level.upcase
end
end

end
Expand Down
25 changes: 25 additions & 0 deletions test/cases/transaction_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ class TransactionTestSQLServer < ActiveRecord::TestCase
connection.user_options_isolation_level.must_match %r{read committed}i
end

describe 'when READ_COMMITTED_SNAPSHOT is set' do
before do
connection.execute "ALTER DATABASE [#{connection.current_database}] SET ALLOW_SNAPSHOT_ISOLATION ON"
connection.execute "ALTER DATABASE [#{connection.current_database}] SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE"
end

after do
connection.execute "ALTER DATABASE [#{connection.current_database}] SET ALLOW_SNAPSHOT_ISOLATION OFF"
connection.execute "ALTER DATABASE [#{connection.current_database}] SET READ_COMMITTED_SNAPSHOT OFF WITH ROLLBACK IMMEDIATE"
end

it 'should use READ COMMITTED as an isolation level' do
connection.user_options_isolation_level.must_match "read committed snapshot"

Ship.transaction(isolation: :serializable) do
Ship.create! name: 'Black Pearl'
end

# We're actually testing that the isolation level was correctly reset to
# "READ COMMITTED", and that no exception was raised (it's reported back
# by SQL Server as "read committed snapshot").
connection.user_options_isolation_level.must_match "read committed snapshot"
end
end


protected

Expand Down