嵌套事务范围,必需 -> 抑制 -> 必需

Kro*_*oaX 4 .net c# sql-server transactions transactionscope

内部 TransactionScope 将哪个事务作为环境事务?

using ( var t1 = new TransactionScope( TransactionScopeOption.Required ) )
{
    //MyStuff

    using ( var t2 = new TransactionScope( TransactionScopeOption.Suppress) )
    {
        //MyStuff

        using ( var t3 = new TransactionScope( TransactionScopeOption.Required) )
        {
            //Mystuff

            t3.Complete();
        }

        t2.Complete();
    }

    t1.Complete();
}
Run Code Online (Sandbox Code Playgroud)

Rem*_*anu 5

t3。没有其他选择,因为 t2 的范围是抑制 t1,而不是创建它自己的环境。因此,在最内层的作用域中只有 t3 而没有别的。

如果 t2 是RequireNew那么最里面的范围环境将是 t2,因为 t3 将加入 t2。