实体框架:如何在SQL事件探查器中查找事务隔离级别?

时间:2022-11-14 09:02:08

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):

BeginTransaction方法用于管理Entity Framework 6中的事务。它允许为事务设置隔离级别,如下面的代码所示(仅示例):

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.

问题是:当我使用SQL Server Profiler时,我找不到任何有关真正SQL事务的隔离级别的信息。

Attempt #1:

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

我试图跟踪所有类型的事件,并在跟踪结果中使用“isolation”关键字进行搜索。我找到的只有两件事:

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.

READ COMMITTED始终处于这些事件中。所以这不是我的代码,因为上面设置了IsolationLevel.Serializable。

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:

对于已启动但尚未提交的事务,可以从dm_exec_sessions视图中获取SPID并手动选择实际隔离级别:

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?

是否可以直接在Profiler中记录任何EF生成的事务/会话的隔离级别?也许我只是使用错误的工具?

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

附:实体框架6.1.3和MS SQL Server 2012。

1 个解决方案

#1


0  

You cannot find a transaction isolation level in profiler as it is never recorded. See How to monitor transaction isolation level changes in SQL Profiler or in any other tool for explanation.

您无法在Profiler中找到事务隔离级别,因为它从未被记录。有关说明,请参见如何在SQL事件探查器或任何其他工具中监视事务隔离级别更改。

#1


0  

You cannot find a transaction isolation level in profiler as it is never recorded. See How to monitor transaction isolation level changes in SQL Profiler or in any other tool for explanation.

您无法在Profiler中找到事务隔离级别,因为它从未被记录。有关说明,请参见如何在SQL事件探查器或任何其他工具中监视事务隔离级别更改。