Timeout with TransactionScopeOptions.Suppress
Asked Answered
D

2

7

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?

Draggletailed answered 15/4, 2013 at 18:23 Comment(0)
B
1

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

Benito answered 15/4, 2013 at 18:35 Comment(1)
typo - within unscoped, rather than within scoped. Also, the dirty trick with "forgetting" to call Complete could perhaps be better explained in the comment.Alignment
A
0

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.

Alignment answered 30/8, 2023 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.