AgentFire
AgentFire

Reputation: 9790

Will the nested transactions be rolled back when the root one rolls back?

In this code..

public static TransactionScope CreateTransactionScope(bool createNew = false)
{
    return new TransactionScope(
        createNew ? TransactionScopeOption.RequiresNew : TransactionScopeOption.Required,
        new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted });
}

Actually, in this one...

using (TransactionScope rootScope = CreateTransactionScope())
{
    using (TransactionScope nestedOne = CreateTransactionScope())
    { nestedOne.Complete(); }

    using (TransactionScope nestedTwo = CreateTransactionScope(true))
    { nestedTwo.Complete(); }

    // No committing, rollback 'rootScope'.
}

What transactions will be rolled back along with the root one - will it be only nestedOne or both nestedOne and nestedTwo?

Upvotes: 4

Views: 4667

Answers (1)

Tamir
Tamir

Reputation: 3911

nestedOne will join the root scope, so if the root scope will rollback, nestedOne will be roll back as well, but not nestedTwo which is a seperate transaction.

like you have the "RequireNew" option that seperate the transaction from the enclosing transaction you can have the "Suppress" option that stops the transaction for that scope.

Take a look at the following list from MSDN that gives a great lesson about transactions behaviour. http://msdn.microsoft.com/en-us/library/ms172152(v=vs.90).aspx

Upvotes: 7

Related Questions