Search code examples
c#.netsql-servertransactionstransactionscope

Nested Transactionscope , Required -> Suppress -> Required


Which Transaction does the inner TransactionScope take as ambient transaction ?

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();
}

Solution

  • t3. There is nothing else to choose from there, since the scope of t2 is to supress t1, not to create it's own ambient. Therefore at the innermost scope there is only t3 and nothing else.

    If t2 would be RequireNew then the innermost scope ambient would be t2 since t3 would join t2.