gpt4 book ai didi

c# - 如何使用多个、嵌套、transactionscope?

转载 作者:行者123 更新时间:2023-11-30 12:18:58 25 4
gpt4 key购买 nike

我正在 linq2sql 中执行一些需要在事务中运行的操作。但是,我在事务内部使用的一些方法也使用 linq2sql 并在它们自己的事务内部运行(内部事务在存储过程中运行)。这给了我异常(exception)

[TransactionInDoubtException: The transaction is in doubt.]
System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx) +76
with the inner exception
[SqlException (0x80131904): There is already an open DataReader associated with this Command which must be closed first.]

如果我对 SQL Server 使用 MultipleActiveResultSets,我反而会得到异常

[SqlException (0x80131904): The transaction operation cannot be performed because there are pending requests working on this transaction.]

有没有人有以这种方式使用 linq2sql 和 transactionscopes 的经验?

最佳答案

这对我来说是一个“打脸”的时刻,但考虑到我看到了这种确切的行为,而且它并没有立即击中我,我想我会继续发布这个作为一种可能性:

当我为 ReadUncommitted 设置 TransactionScope 时,我看到了这种行为:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, 
new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted })

我的 Linq to SQL 正在调用存储过程以返回过程的结果。面对面的是,在 proc 本身中,可以指定 WITH (NOLOCK) SQL 提示,因此无需将 Linq to SQL 查询包装在 ReadUncommitted 事务范围内。 (至少在我的情况下)

关于c# - 如何使用多个、嵌套、transactionscope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/971080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com