Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: how to find transaction isolation level in SQL Profiler?

BeginTransaction method is used to manage transactions in Entity Framework 6. It allows to set isolation level for transaction as you may see in code below (just a sample):

using (var context = new DataContext())
{
    using (var transaction = context.Database.BeginTransaction(IsolationLevel.Serializable))
    {
        context.Entities.Add(new Entity());

        context.SaveChanges();

        transaction.Commit();
    } 
}

The problem is: when I use SQL Server Profiler, I cann't find any information about isolation level for real SQL transaction.

Attempt #1:

I tried to trace ALL kinds of events and search by "isolation" keyword in trace results. Only two events I found:

EventClass          TextData
-------------------------------------------------------------
ExistingConnection  set transaction isolation level read committed
AuditLogin          set transaction isolation level read committed

READ COMMITTED is always in these events. So it is not about my code, because IsolationLevel.Serializable is set above.

Attempt #2:

For the transaction has been started and not committed yet, it is possible to take SPID and manually select real isolation level from dm_exec_sessions view:

SELECT transaction_isolation_level
FROM sys.dm_exec_sessions
WHERE session_id = @Tran_SPID

This is very undesirable way.

Is it possible to record isolation level for any EF-generated transaction/session in profiler directly? Maybe I just use wrong tool?

P.S. Entity Framework 6.1.3 and MS SQL Server 2012 on board.

like image 600
Ilya Chumakov Avatar asked Jul 14 '15 16:07

Ilya Chumakov


People also ask

How can check transaction isolation level in SQL Server?

To check the isolation level(s) present in the statement, the simplest way is to look at the T-SQL itself and see if any hints are present. If not, it is operating at the isolation level of the connection.

How do I set isolation level in Entity Framework?

We can set Isolation Level using the ExecuteStoreCommand method of the context. It would affect generated LINQ to Entity queries. DBContext or Object context also supports explicitly setting the transaction on the context. Using transaction scope we can set transaction Isolation Level for current transaction.

Where is transaction isolation level set?

The isolation level of the transactional support is default to READ UNCOMMITTED. You can change it to READ COMMITTED SNAPSHOT ISOLATION by turning ON the READ_COMMITTED_SNAPSHOT database option for a user database when connected to the master database.

What are the transaction isolation levels in SQL?

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


1 Answers

You picked the wrong tools because with Entity Framework Profiler you can easily check the isolation level (and lots of information about EF behaviour).

This is my transaction sample :

begin transaction with isolation level: ReadCommitted

EF Profiler

More info : https://hibernatingrhinos.com/products/efprof/learn

like image 173
XAMT Avatar answered Sep 21 '22 09:09

XAMT