Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction Isolation Level Scopes

What are the scoping rules for transaction isolation levels in SQL Server 2005? I know what the different levels mean, but not how to properly apply them outside of a manually run script. I can't find a guide for practical use in production-quality code.

Obviously, the scope begins when you use a command like this:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED  

But where does it end? If I set the iso level in a stored procedure and then that proc calls another, does the nested proc inherit it? Even better, if I escalate the iso level inside the nested proc is it going to carry back out into the calling proc? Do transaction commands like BEGIN TRAN, ROLLBACK, and COMMIT make any differences?

When a stored proc is called by an application or an agent job do the isolation level changes persist in some way? Do I always have to revert to the default READ COMMITTED at the end of each proc?

I would test it in different situations but I don't know how to read what the current isolation level is set to.

like image 848
SurroundedByFish Avatar asked Jun 25 '09 19:06

SurroundedByFish


People also ask

What are the four transaction isolation levels?

InnoDB offers all four transaction isolation levels described by the SQL:1992 standard: READ UNCOMMITTED , READ COMMITTED , REPEATABLE READ , and SERIALIZABLE .

What is the purpose of transaction isolation levels?

Transaction isolation levels are a measure of the extent to which transaction isolation succeeds. In particular, transaction isolation levels are defined by the presence or absence of the following phenomena: Dirty Reads A dirty read occurs when a transaction reads data that has not yet been committed.

What is transaction isolation level 2?

Level 2 prevents nonrepeatable reads. These occur when one transaction reads a row and a second transaction modifies that row. If the second transaction commits its change, subsequent reads by the first transaction yield results that are different from the original read.

How do you SELECT transaction isolation level?

A user can change the isolation level for a single session or for all subsequent connections with the SET TRANSACTION statement. To set the server's default isolation level for all connections, use the --transaction-isolation option on the command line or in an option file.


1 Answers

Run the following and see for yourself:

CREATE PROCEDURE dbo.KeepsIsolation AS BEGIN PRINT 'Inside sproc that does not change isolation level'; DBCC USEROPTIONS; END GO  CREATE PROCEDURE dbo.ChangesIsolation AS BEGIN PRINT 'Inside sproc that changes isolation level'; SET TRANSACTION ISOLATION LEVEL READ COMMITTED; DBCC USEROPTIONS; END GO SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; DBCC USEROPTIONS; EXEC dbo.KeepsIsolation; DBCC USEROPTIONS; EXEC dbo.ChangesIsolation; -- demonstrates that isolation level restored to REPEATABLE READ after exiting the procedure     DBCC USEROPTIONS; 
like image 113
A-K Avatar answered Oct 18 '22 13:10

A-K