Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding TransactionScopeOptions: RequiresNew = Suppress + Required?

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
}
like image 231
Jaxidian Avatar asked Aug 08 '11 20:08

Jaxidian


People also ask

What does suppress transaction mean?

Suppressing a transaction removes the transaction from the sequence of steps. The step can be reinserted by reversing the suppression. When suppressed, the translation step is grayed out, and corresponding node removed from the graph view. Deleting a transaction removes the transaction step permanently.

What does Transactionscopeoption suppress do?

Suppress will suppress the current ambient transaction and does not enforce any semantic on scopes further down the stack. If there are other TransactionScopes further down the stack they will react just as if there was no ambient transaction (and create one if necessary).

What is ambient transaction C#?

The ambient transaction is the transaction within which your code executes. You can obtain a reference to the ambient transaction by calling the static Transaction. Current property of the Transaction class.


1 Answers

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

like image 103
Eddy Avatar answered Sep 17 '22 12:09

Eddy