Method1()
实例化一个TransactionScope
并调用Method2()
,后者也实例化一个TransactionScope
时,.NET如何知道两者在同一作用域内?
我认为它不会在内部使用静态方法,否则在像ASP.NET这样的多线程应用程序中可能无法正常工作。是否可以创建类似于TransactionScope的自定义类,还是原始类使用特殊功能只有微软知道如何运作?
Method1()
实例化一个TransactionScope
并调用Method2()
,后者也实例化一个TransactionScope
时,.NET如何知道两者在同一作用域内?
我认为它不会在内部使用静态方法,否则在像ASP.NET这样的多线程应用程序中可能无法正常工作。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(); }
TransactionScope
基本上是建立在 COM 之上的 - 具体来说是在 MSDTC 上。
这个协调事务,并允许嵌套事务。
简而言之,当您首次调用 TransactionScope
时,一个事务将在 MSDTC 中注册,所有对 TransactionScope
的调用都会如此。 MSDTC 协调它们所有。