TransactionScope 如何运作?

发布于 2024-09-13 05:52:45 字数 275 浏览 8 评论 0原文

Method1() 实例化 TransactionScope 并调用同时实例化 TransactionScopeMethod2() 时,.NET 如何执行知道两者都在同一范围内吗? 我相信它在内部不使用静态方法,否则它无法在 ASP.NET 等多线程应用程序上正常工作。

是否可以创建我自己的类似 TransactionScope 的类,或者原始类是否使用了 Microsoft 知道其工作原理的特殊功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖阳 2024-09-20 05:52:45

希望这有帮助:

http://msdn.microsoft.com/en-us/magazine /cc300805.aspx

对于那些不熟悉 TransactionScope 的人来说,它是 Microsoft® .NE​​T Framework 2.0 中新增的 System.Transactions 命名空间的一部分。 System.Transactions 提供完全集成到 .NET Framework 中的事务框架,包括但不限于 ADO.NET。 Transaction 和 TransactionScope 类是此命名空间中最重要的两个类。正如问题所提到的,您可以创建一个 TransactionScope 实例,并且在该 TransactionScope 范围内执行的 ADO.NET 操作将自动登记(您还可以通过 Transaction.Current 静态属性访问当前事务):

using(TransactionScope 范围 = new TransactionScope())
{
    ... // 这里的所有操作都是事务的一部分
    范围.Complete();
}

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();
}
贵在坚持 2024-09-20 05:52:45

TransactionScope 几乎建立在 COM 之上 - 特别是在 MSDTC 之上。

这可以协调事务,并允许嵌套事务。

简而言之,当您第一次调用 TransactionScope 时,事务会向 MSDTC 注册,就像对 TransactionScope 的所有其他调用一样。 MSDTC 对它们进行协调。

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.

剪不断理还乱 2024-09-20 05:52:45

答案是,执行可由 TransactionScope 保护、提交和回滚的操作的类必须进行专门编码,以了解“环境”事务。 (这不是魔法。)这个答案很好地解释了这一点:How to enlist with a TransactionScope?< /a>

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文