TransactionScopeOption.RequiresNew 的 TransactionScope 超时怪异
这里发生了一些非常奇怪的事情。
我刚刚在我正在调试的一些遗留代码周围添加了一个事务范围,以确保我所做的摆弄不会被提交。
这工作了两次,然后说:
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
之间的区别
和
在于,第二个重用现有(环境)事务,而第一个在旧事务中创建新事务。
事务中的事务需要分布式事务协调器。
导致错误的可能原因有以下三个:
The difference between:
and
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:
我相信,当错误发生时,框架正在尝试将原始的“轻量级”(即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.
老问题,但我遇到了同样的问题,所以这里提供了解决多种问题的简单信息。
如果您可以从
TransactionScopeOption.RequiresNew
更改为TransactionScopeOption.Required
检查您是否确实需要显式嵌套事务的行为并考虑显式设置通过阅读 这篇 MSDN 文章了解事务范围。 也许您的问题是锁定资源的新事务的数量,这就是问题“奇怪”的原因。此外,您应该检查以下信息:
默认事务范围被认为是有害的,
默认情况下您设置:
IsolationLevel.ReadCommited
)使用这两个选项和
TransactionScopeOption.RequiresNew
您应该会遇到死锁。这是新代码:
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
toTransactionScopeOption.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
by default you set:
IsolationLevel.ReadCommitted
)TransactionManager.MaximumTimeout
)With those two options and the
TransactionScopeOption.RequiresNew
you should expect deadlocks.Here the new code: