Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isolation level in C# or SQL - which one will be used?

Tags:

I have set isolation level in C# code as readcommitted, and I am calling a stored procedure which is timing out due to some reason. The stored procedure does not have any set isolation level statement.

In SQL Server, database level isolation level is read committed snapshot.

So which isolation level will be used? The one defined in SQL Server, or the one set from C#?

like image 597
Sagar Avatar asked May 21 '19 18:05

Sagar


People also ask

What is isolation level in database?

What is an “Isolation Level”? Database isolation refers to the ability of a database to allow a transaction to execute as if there are no other concurrently running transactions (even though in reality there can be a large number of concurrently running transactions).

What is the best isolation level?

The highest isolation level, serializable, guarantees that a transaction will retrieve exactly the same data every time it repeats a read operation. But it uses a level of locking that is likely to impact other users in multi-user systems.

What is default isolation level?

The default isolation level is REPEATABLE READ . Other permitted values are READ COMMITTED , READ UNCOMMITTED , and SERIALIZABLE . For information about these isolation levels, see Section 14.7.


1 Answers

There is no such thing as a 'database isolation level'. What you describe is a database options, called READ_COMMITTED_SNAPSHOT:

READ_COMMITTED_SNAPSHOT { ON | OFF } ON Enables Read-Committed Snapshot option at the database level. When it's enabled, DML statements start generating row versions even when no transaction uses Snapshot Isolation. Once this option is enabled, the transactions specifying the read committed isolation level use row versioning instead of locking.

So when READ_COMMITTED_SNAPSHOT is ON a transaction that specified read committed isolation level will instead see a snapshot isolation level.

It is important to understand that there is another database option: ALLOW_SNAPSHOT_ISOLATION that also must be set to ON for Snapshot isolation to occur. See Snapshot Isolation in SQL Server.

When in doubt, you can always check sys.dm_tran_current_transaction which has a column named transaction_is_snapshot:

Snapshot isolation state. This value is 1 if the transaction is started under snapshot isolation. Otherwise, the value is 0.

Also, there are subtle differences between true snapshot isolation level and read committed isolation that is changed to snapshot by READ_COMMITTED_SNAPSHOT.

like image 143
Remus Rusanu Avatar answered Oct 27 '22 09:10

Remus Rusanu