Nested/Child TransactionScope Rollback

Posted by Robert Wagner on Stack Overflow See other posts from Stack Overflow or by Robert Wagner
Published on 2010-04-30T02:36:05Z Indexed on 2010/04/30 2:37 UTC
Read the original article Hit count: 675

I am trying to nest TransactionScopes (.net 4.0) as you would nest Transactions in SQL Server, however it looks like they operate differently. I want my child transactions to be able to rollback if they fail, but allow the parent transaction to decide whether to commit/rollback the whole operation. A greatly simplified example of what I am trying to do:

static void Main(string[] args)
{
    using(var scope = new TransactionScope()) // Trn A
    {
        // Insert Data A

        DoWork(true);
        DoWork(false);

        // Rollback or Commit
    }
}

// This class is a few layers down
static void DoWork(bool fail)
{
    using(var scope = new TransactionScope()) // Trn B
    {
        // Update Data A

        if(!fail)
        {
            scope.Complete();
        }
    }
}

I can't use the Suppress or RequiresNew options as Trn B relies on data inserted by Trn A. If I do use those options, Trn B is blocked by Trn A.

Any ideas how I would get it to work, or if it is even possible using the System.Transactions namespace?

Thanks

© Stack Overflow or respective owner

Related posts about .NET

Related posts about transactions