使用 TransactionScope 和 MS DTC 时的常见问题

发布于 2024-08-12 03:17:49 字数 253 浏览 4 评论 0原文

我刚刚开始使用 TransactionScope,我发现总是遇到一些意想不到的事情,需要永远调试。

我认为,拥有这些内容的综合列表对于那些“奇怪的错误”情况非常有用,并且可以扩展我们对平台中奇怪现象的了解。

关于我将如何使用事务范围的一些上下文:

  • Web 应用程序
  • 多个 Web 服务器、应用程序服务器和 SQL 服务器
  • 事务将主要是数据库事务,但有些事务将提升为写入 MSMQ。

I am just starting to work with using TransactionScope, I find that there are always unexpected things I run into that take forever to debug.

I figure that having a consolidated list of these would be great for those "weird error" circumstances, plus to expand our knowledge of oddness in the platform.

Some context on how I am going to be using transaction scopes:

  • web application
  • multiple web servers, application servers and sql servers
  • transactions will be mainly database transactions but some will be elevated to write to MSMQ.

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

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

发布评论

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

评论(3

守不住的情 2024-08-19 03:17:49

我突然想到两件事:

  • 当您在同一范围内使用多个连接对象时,即使连接具有相同的连接字符串(这在 sql 2008 中已修复),事务也会提升。请阅读此帖子dbconnectionscope 将解决 sql 2005
  • msdtc 实例需要能够看到的 问题彼此都需要正确设置安全性
    http://support.microsoft.com/kb/899191(允许入站和出站,请执行不需要相互身份验证通常是最安全的选择)。使用 DTCPing 解决 dtc 实例之间的连接问题,如下所述:http://support.microsoft.com/kb /306843

你希望事务尽可能的轻量级,dtc引入了大量的开销。您还希望事务尽可能短,因此仅在应用程序服务器上引入它们,而不是在 Web 服务器上引入它们。使应用程序服务器和数据库之间的网络跃点尽可能小且尽可能快,通过与应用程序服务器和数据库之间不同的连接在 Web 和应用程序服务器之间发送网络流量,并使最后一个速度快得惊人,连接短得可笑。

如果您有多个应用程序服务器,您可以考虑在一台服务器上运行一个 msdtc 实例(例如在数据库上或在其中一个应用程序服务器上),并从所有应用程序服务器远程使用它,而不是每个应用程序服务器都运行自己的服务器,但是我不知道这还有什么额外的好处。

2 things off the top of my head:

  • transactions will be elevated when you use more than one connection object in the same scope, even if the connections have the same connectionstring (this is fixed in sql 2008). Read more in this thread and dbconnectionscope will solve that problem on sql 2005
  • msdtc instances need to be able to see each other and need to have their security set up correctly
    http://support.microsoft.com/kb/899191 (allow inbound and outbound, do not require mutual authentication is usually the safest bet). Use DTCPing to troubleshoot connection issues between dtc instances as explained here: http://support.microsoft.com/kb/306843

You want transactions to be lightweight as much as possible, dtc introduces a lot of overhead. You also want transactions to be as short as possible, so introduce them only on the application servers and not on the web server. Make the hop over the network between the application servers and the database as small as possible and as fast as possible, send network traffic between web and app servers over a different connection than between app servers and db, and make the last one a screaming fast, ridiculously short connection.

If you have multiple application servers, you could consider having a single instance of msdtc running on a server (e.g. on the database or on one of the app servers) and use this remotely from all application servers instead of each one running their own, but I don't know what additional benefits this has.

从此见与不见 2024-08-19 03:17:49

希望有一天这会对某人有所帮助:

就不会涉及 DTC

  1. 如果您的 TransactionScope 内部包含多个 SQL 操作,则只要您为每个连接使用相同的连接字符串
  2. 而不是嵌套连接,

。即打开,做某事,关闭。打开,做某事,关闭。

现在来说说陷阱:
如果您在进程中(在另一个线程上)

SqlConnection.ClearAllPools()

执行此操作,并且恰好发生在您的两个操作之间 - DTC 将立即涉及。如果没有运行 DTC,它将抛出异常。

Hopefully this will help someone one day:

If you have a TransactionScope with multiple SQL operations inside, the DTC will not be involved provided

  1. you use identical connection string for each connection
  2. connections are not nested.

I.e. open, do someting, close. open, do something, close.

Now for the gotcha:
if you ever do this in your process (on another thread)

SqlConnection.ClearAllPools()

and that happens to come between your two operations - the DTC will be involved immediately. If you don't have the DTC running, it will throw an Exception.

蘸点软妹酱 2024-08-19 03:17:49

如果您使用 SQL Server 并检查 @@trancount,即使您有活动的 TransactionScope,它也将为 0。

If you use SQL Server and check @@trancount, it will be 0 even if you have an active TransactionScope.

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