TransactionScopeOption.RequiresNew 的 TransactionScope 超时怪异

发布于 2024-07-18 13:19:53 字数 703 浏览 5 评论 0原文

这里发生了一些非常奇怪的事情。

我刚刚在我正在调试的一些遗留代码周围添加了一个事务范围,以确保我所做的摆弄不会被提交。

这工作了两次,然后说:

"The transaction manager has disabled its support for remote/network transactions."

在工作/非工作之间没有任何代码更改或重建(实际上是连续 3 个 F5 [网络应用程序])。 这是连接到删除数据库服务器的本地代码。

因此,不同项目中完全独立的代码就会超时。 如果我从这段代码中删除 transactionScopes,它运行得很好,但使用它们后就会超时。 我已经尝试了本地 SQL 服务器和远程 SQL 服务器,两者都在 transactionScope 内超时。

这到底是怎么回事?

编辑:我发现将我的 TransactionScopes 从:更改

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))

为可以

using (var scope = new TransactionScope())

防止出现问题:s

这是什么意思?

Something ultra strange is going on here.

I just added a transaction scope around some legacy code I was debugging to ensure the fiddling I was doing wouldn't get committed.

This worked twice, then said:

"The transaction manager has disabled its support for remote/network transactions."

without any code changes or rebuilds between the working/nonworkingness (literally 3 F5s in a row [web app]). This was local code connecting to a remove DB server.

Since this, completely separate code in a different project is timing out. If I remove the transactionScopes from this code it runs fine, but with them in place it times out. I've tried my local SQL server and remote ones, both time out inside the transactionScope.

What the hell is going on?

Edit: I've found that changing my TransactionScopes from:

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))

to

using (var scope = new TransactionScope())

prevents the problem :s

What does this mean?

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

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

发布评论

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

评论(3

独行侠 2024-07-25 13:19:53

之间的区别

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))

using (var scope = new TransactionScope())

在于,第二个重用现有(环境)事务,而第一个在旧事务中创建新事务。

事务中的事务需要分布式事务协调器。

导致错误的可能原因有以下三个:

  • MSDTC 未运行
  • 您的数据库位于另一台计算机上,并且 Windows 配置为不允许来自网络的事务。
  • 您的数据库位于另一台计算机上,并且 SQL Server 配置为不允许来自网络的事务

The difference between:

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))

and

using (var scope = new TransactionScope())

Is that the second one reuse the existing (ambient) transaction, whereas the first one creates a new transaction within an old one.

This transaction within a transaction requires the Distributed Transaction Coordinator.

There are then three probable reasons for your error:

  • The MSDTC is not running
  • Your database is on a different machine and windows is configured not to allow transactions from the network.
  • Your database is on a different machine and SQL server is configured not to allow transactions from the network
温柔戏命师 2024-07-25 13:19:53

我相信,当错误发生时,框架正在尝试将原始的“轻量级”(即DBMS)事务提升为“分布式”事务。 管理分布式事务的 MSDTC(分布式事务协调器)服务未运行或无法工作。

当一个逻辑事务跨越两个(或更多)数据库连接时,通常会发生这种情况。 当然,在您的情况下(显然)只有一个 DBMS 连接。 我猜测通过强制一个新的、独立的事务范围,您也强制框架使用分布式事务。

I believe what is happening when the error occurs is that the framework is trying to promote the original "lightweight" (i.e. DBMS) transaction to a "distributed" transaction. The MSDTC (Distributed Transaction Coordinator) service, which manages distributed transactions, is either not running or is otherwise not able to work.

This typically happens when one logical transaction spans two (or more) database connections. Of course, in your case there is (apparently) only one DBMS connection. I'm guessing by forcing a new, independent transaction scope you are also forcing the framework to use a distributed transaction.

等往事风中吹 2024-07-25 13:19:53

老问题,但我遇到了同样的问题,所以这里提供了解决多种问题的简单信息。

如果您可以从 TransactionScopeOption.RequiresNew 更改为 TransactionScopeOption.Required 检查您是否确实需要显式嵌套事务的行为并考虑显式设置通过阅读 这篇 MSDN 文章了解事务范围。 也许您的问题是锁定资源的新事务的数量,这就是问题“奇怪”的原因。

此外,您应该检查以下信息:

默认事务范围被认为是有害的

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))

默认情况下您设置:

  1. IsolationLevel 为可序列化(考虑使用 IsolationLevel.ReadCommited
  2. 超时为一分钟(考虑使用 < (code>TransactionManager.MaximumTimeout)

使用这两个选项和 TransactionScopeOption.RequiresNew 您应该会遇到死锁。

这是新代码:

new TransactionScope(TransactionScopeOption.Required, 
    new TransactionOptions 
    {
        IsolationLevel = IsolationLevel.ReadCommitted,
        Timeout = TransactionManager.MaximumTimeout
    }
)

Old question but i had the same problem so here the simple information to fix many kind of issues.

If you can change from TransactionScopeOption.RequiresNew to TransactionScopeOption.Required check if you really need to explicit the behaviour for nested transactions and consider set explicitly the kind of Transaction scope by reading this MSDN article. Probably your problem was the number of new transactions with locked resources, thats why the problem was "weird".

Additionally you should check this info:

The default transaction scope is considered harmful

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))

by default you set:

  1. IsolationLevel as Serializable (consider using IsolationLevel.ReadCommitted)
  2. TimeOut to one minute (consider using TransactionManager.MaximumTimeout)

With those two options and the TransactionScopeOption.RequiresNew you should expect deadlocks.

Here the new code:

new TransactionScope(TransactionScopeOption.Required, 
    new TransactionOptions 
    {
        IsolationLevel = IsolationLevel.ReadCommitted,
        Timeout = TransactionManager.MaximumTimeout
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文