32

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?

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
Eduardo
  • 5,645
  • 4
  • 49
  • 57
  • 3
    a bit off topic so a comment: static methods aren't a big deal for multithreading, it's static DATA that you have to worry about – µBio Aug 05 '10 at 18:37

3 Answers3

20

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();
}
gnat
  • 6,213
  • 108
  • 53
  • 73
Jordão
  • 55,340
  • 13
  • 112
  • 144
  • 1
    Sure!!! They may use Thread.SetData() also: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28SYSTEM.THREADING.THREAD.SETDATA%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29;k%28DevLang-CSHARP%29&rd=true – Eduardo Aug 09 '10 at 12:35
  • @MiklX: that's really bad, try the [full September 2006](http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazineSeptember2006en-us.chm) issue. I'll take a better look at this later. – Jordão Jun 13 '15 at 21:18
9

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 2
    Sometimes transactions (specially database ones) are promoted to DTC but not always. – Eduardo Aug 06 '10 at 12:34
  • @Eduardo - that's true. The LTM (Lightweight Transaction Manager may promote a transaction to distributed via MSDTC). – Oded Aug 06 '10 at 12:50
0

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?

Jordan Rieger
  • 3,025
  • 3
  • 30
  • 50