Reputation: 5933
When Method1()
instantiates a TransactionScope
and calls Method2()
that also instantiates a TransactionScope
, how does .NET know both are in the same scope?
I believe it doesn't use static methods internally otherwise it wouldn't work well on multithreaded applications like ASP.NET.
Is it possible to create my own TransactionScope-like class or does the original one use special features those just Microsoft knows how they work?
Upvotes: 32
Views: 17525
Reputation: 2664
The answer is that the classes that perform the actions that can be protected, committed, and rolled back by the TransactionScope have to be specifically coded to be aware of the "ambient" transaction. (It's not magic.) This answer explains it well: How to enlist with a TransactionScope?
Upvotes: 0
Reputation: 56457
Hope this helps:
http://msdn.microsoft.com/en-us/magazine/cc300805.aspx
For those unfamiliar with TransactionScope, it is part of the System.Transactions namespace new to the Microsoft® .NET Framework 2.0. System.Transactions provides a transactions framework fully integrated into the .NET Framework, including but not limited to ADO.NET. The Transaction and TransactionScope classes are two of the most important classes in this namespace. As the question alludes to, you can create a TransactionScope instance, and ADO.NET operations executed within the scope of that TransactionScope will be enlisted automatically (you can also access the current Transaction through the Transaction.Current static property):
using(TransactionScope scope = new TransactionScope()) { ... // all operations here part of a transaction scope.Complete(); }
Upvotes: 20
Reputation: 498904
TransactionScope
pretty much builds on top of COM - specifically over MSDTC.
This coordinates transactions, and allows nesting of transactions.
In short, when you first call TransactionScope
, a transaction registers with MSDTC, as would all other calls to TransactionScope
. MSDTC coordinates them all.
Upvotes: 9