Sign
Sign

Reputation: 1959

Timeout with TransactionScopeOptions.Suppress

Found this line in an application I just took over, and it doesn't make much sense.

using (new TransactionScope(TransactionScopeOption.Suppress, new TimeSpan(1,0,0))) {

This occurs immediately inside a nservicebus message handler method and covers the entire handler.

It appears to be trying to suppress the ambient transaction and after an hour aborting. What happens when the timeout expires? I assume this is just a combination of options that doesn't mean anything reasonable. But what does it result in happening?

Upvotes: 7

Views: 1175

Answers (2)

Peter Ritchie
Peter Ritchie

Reputation: 35881

Suppress means that the ambient transaction is not used; and that, in affect, the operations within the scope are not performed in a transaction. This allows you to execute operations outside of the current transaction without being affected by that transaction. e.g.:

using(var trans = new TransactionScope())
{
   // do operations within transaction
   using(var unscoped = new TransactionScope(TransactionScopeOption.Suppress))
   {
      // do "immediate" operations
   }
   // do operations within transaction
   // NOTE: No trans.Complete() called
}

// operations performed within `unscoped` are not rolled back.

I'm not really sure if the timeout really makes any sense with Suppress

Upvotes: 1

Jirka Hanika
Jirka Hanika

Reputation: 13529

From MSDN:

If the scope is instantiated with Suppress, it never takes part in a transaction, regardless of whether an ambient transaction is present. A scope instantiated with this value always have null as its ambient transaction.

The timeout setting has no effect with Suppress. The two can be combined only because the set of TransactionScope constructors is static and it cannot prevent the combination from being specified.

For a "nested" transaction scope, specifying the timeout can reduce, but not increase, the timeout on the ambient transaction.

In contrast, the TransactionScope instantiated with Suppress never joins any ambient transaction, nor forms a new one, and therefore nobody's transaction timeout is ever affected.

Upvotes: 0

Related Questions