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
}
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With