17

I believe I understand TransactionScopeOption.Suppress and TransactionScopeOption.Required but am having difficulty understanding what TransactionScopeOption.RequiresNew does. Based on the last explanation that I read, would the following two blocks of code functionally be the same? Is this an accurate representation of what RequiresNew means?

using (var ts1 = new TransactionScope(TransactionScopeOption.RequiresNew))
{
  DoStuff();
  ts1.Complete();
}

and

using (var ts2 = new TransactionScope(TransactionScopeOptions.Suppress))
{
  using (var ts3 = new TransactionScope())
  {
    DoStuff();
    ts3.Complete();
  }

  ts2.Complete(); // not required but recommended for consistency's sake
}
Jaxidian
  • 13,081
  • 8
  • 83
  • 125

1 Answers1

15

To get a good understanding of the transaction scopes you can read this msdn article

I can't find a good explanation how those two would be different except that the number of nested scopes that are created are different. Both cases should lead to the same amount of transactions regardless if a transaction already exists or not. I can't find a good resource to refer to but I would always go for RequiresNew over a combined Suppress/Required. RequiresNew basically means: "regardless if there already is or isn't a transaction give me a new one".

Update: In case the first link remains broken you can find it in the wayback archive here

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Eddy
  • 5,320
  • 24
  • 40
  • Already did, that's what gave me this impression. Quotes: "`RequiresNew`: the contained code depends on a transaction, but must be independently committed or rolled back. `Suppress`: the contained code performs its own compensation, so must not be part of a transaction." This means (to me) "committing" or "rolling back" a RequiresNew TransactionScope cannot be manipulated or dependent upon an outside transaction - neither one affects the other. That's what the Suppress should do to its inside Required TransactionScope. – Jaxidian Aug 08 '11 at 20:37
  • In all honesty I misread your second code block and thought you had 2 nested transactionscopes() (I missed the Suppress on the outer one). I will edit – Eddy Aug 08 '11 at 20:59
  • 2
    And just to clarify... If I rollback the RequiresNew transaction, that in no way affects a parent TransactionScope, and likewise, if I rollback a parent TransactionScope, that in no way affects the RequiresNew transaction? Sorry, just want to make sure I understand these options fully. Lots of potentially nasty side-effects (deadlock, non-atomic operations, etc) if I get these mixed up! – Jaxidian Aug 08 '11 at 23:02
  • 2
    The first link to the blog entry is broken. – adamjford Jun 05 '12 at 16:53