在.net中使用TransactionScopeOption.Suppress时,为什么忽略隔离级别

时间:2020-12-24 02:33:42

I'm trying to execute an query with the isolation level read uncommitted within an existing transaction using LINQ TO SQL. If I use the option to suppress this transaction from the parent transaction, it seems I lose the ability to specify the isolation level. Using this code in LINQPad:

我正在尝试使用LINQ TO SQL在现有事务中使用未提交的隔离级别读取执行查询。如果我使用该选项从父事务中禁止此事务,似乎我失去了指定隔离级别的能力。在LINQPad中使用此代码:

void Main()
{
var db = this;
db.ExecuteQuery<string>(@"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");

using (var trans = new TransactionScope(TransactionScopeOption.Required))
{
    // updates or inserts here

    using (new TransactionScope(TransactionScopeOption.Suppress,
        new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
    {
        db.ExecuteQuery<string>(@"SELECT CASE transaction_isolation_level 
        WHEN 0 THEN 'Unspecified' 
        WHEN 1 THEN 'ReadUncomitted' 
        WHEN 2 THEN 'Readcomitted' 
        WHEN 3 THEN 'Repeatable' 
        WHEN 4 THEN 'Serializable' 
        WHEN 5 THEN 'Snapshot' END AS TRANSACTION_ISOLATION_LEVEL 
        FROM sys.dm_exec_sessions 
        where session_id = @@SPID
        ").Dump("Inside the transaction");

    }
    // updates or inserts here
}
}

I get the result of SERIALIZABLE. Is there any way to run a query inside a transaction and change the isolation level to read uncommitted?

我得到了SERIALIZABLE的结果。有没有办法在事务中运行查询并将隔离级别更改为读取未提交?

1 个解决方案

#1


3  

What you want to use is TransactionScopeOption.RequiresNew. The Supress option causes your enclosing block to run without an ambient transaction and also without creating a new one. That's what it does.

您要使用的是TransactionScopeOption.RequiresNew。 Supress选项使您的封闭块在没有环境事务的情况下运行,也无需创建新的封闭块。这就是它的作用。

RequiresNew causes a new, root transaction scope to be created.

RequiresNew导致创建新的根事务范围。

See the summary table in this article on how different options behave.

请参阅本文中的摘要表,了解不同选项的行为方式。

More on Suppress:

更多关于抑制:

Suppress is useful when you want to preserve the operations performed by the code section, and do not want to abort the ambient transaction if the operations fail. For example, when you want to perform logging or audit operations, or when you want to publish events to subscribers regardless of whether your ambient transaction commits or aborts.

当您想要保留代码部分执行的操作时,抑制非常有用,并且如果操作失败,则不希望中止环境事务。例如,当您要执行日志记录或审核操作时,或者您希望将事件发布给订阅者时,无论您的环境事务是提交还是中止。

#1


3  

What you want to use is TransactionScopeOption.RequiresNew. The Supress option causes your enclosing block to run without an ambient transaction and also without creating a new one. That's what it does.

您要使用的是TransactionScopeOption.RequiresNew。 Supress选项使您的封闭块在没有环境事务的情况下运行,也无需创建新的封闭块。这就是它的作用。

RequiresNew causes a new, root transaction scope to be created.

RequiresNew导致创建新的根事务范围。

See the summary table in this article on how different options behave.

请参阅本文中的摘要表,了解不同选项的行为方式。

More on Suppress:

更多关于抑制:

Suppress is useful when you want to preserve the operations performed by the code section, and do not want to abort the ambient transaction if the operations fail. For example, when you want to perform logging or audit operations, or when you want to publish events to subscribers regardless of whether your ambient transaction commits or aborts.

当您想要保留代码部分执行的操作时,抑制非常有用,并且如果操作失败,则不希望中止环境事务。例如,当您要执行日志记录或审核操作时,或者您希望将事件发布给订阅者时,无论您的环境事务是提交还是中止。