0

How can I force a minimum isolation level in a procedure in mssql?

I can use "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;" inside my procedure to set the transaction-level. But this command will downgrade the isolation level if the caller/executor uses e.g. "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;" How can I force a minimum isolation level without downgrading a higher isolation level?

What happens if I commit the "nested" transaction in my procdure? Will the locks acquired by the nested transaction remain until the outer transaction (of the caller) is committed?

4
  • 1. Minimum TIL is read uncommited and none of 2 from your question. 2. To force read uncommitted you can use nolock hint. 3. You cannot commit your "nested" transaction, this will only decrease @@trancount by 1, no real commit will be done
    – sepupic
    Commented Sep 25, 2019 at 13:57
  • Anyway, if you want to use repeatable read in your query while serializable is set at the session level, you should use query hint REPEATABLEREAD for every table in your query
    – sepupic
    Commented Sep 25, 2019 at 14:00
  • You got me wrong: My procedure REQUIRES "REPEATABLE READ" to work properly. The procedure is used (called) by another query. If the caller uses a TIL lower than "REPEATABLE READ" and I do nothing -> Bad things happen. If I set the transaction level in my procedure to "REPEATABLE READ" but the caller itself used a higher TIL the command will downgrade from "SERIALIZABLE" to "REPEATABLE READ" -> Not what I wanted. I want a MINIMUM TIL required for my procedure and not a downgrade of a higher TIL Commented Sep 25, 2019 at 14:05
  • I first unswered about "minimum TIL", there was no word about "minimum TIL YOU require in OP. Then I added my second comment where I explained how you can use repeatable read in your transaction even if serializable is used at the session level. USE QUERY LEVEL REPEATABLEREAD
    – sepupic
    Commented Sep 25, 2019 at 14:11

2 Answers 2

2

There are a couple of options for determining the transaction isolation level in SQL Server depending on whether you want to include read committed snapshot as separate from read committed. Once you know what isolation level your session is using, you can implement whatever logic you want. Something like

IF EXISTS( SELECT 1
             FROM sys.dm_exec_sessions 
            WHERE session_id = @@SPID
              AND transaction_isolation_level < 3 )
BEGIN
  set transaction isolation level repeatable read;
END;
0

How can I force a minimum isolation level without downgrading a higher isolation level?

You can indicate required TIL for every table directly in your query by using quary hints:

READCOMMITTED   
READUNCOMMITTED   
REPEATABLEREAD   
SERIALIZABLE  

More on query hints here: Hints (Transact-SQL) - Table

By using hints at quary level you don't alter TIL at the session level.

What happens if I commit the "nested" transaction in my procdure? Will the locks acquired by the nested transaction remain until the outer transaction (of the caller) is committed?

The only thing you'll get by doing commit of the nested transaction is decreasing @@trancount by one. Here you can find more on "nested" transactions: A SQL Server DBA myth a day: (26/30) nested transactions are real

No lock will be released.

But the locks will be different when using query hints. For example you'll have no range locks if you use other than serializable hint in your query.

You can even use nolock or readuncommitted in your query and no shared locks will be acquired even if session level TIL is still serializable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.