【发布时间】:2012-10-31 10:37:42
【问题描述】:
我们正在运行分布式事务,在极少数情况下我们会收到以下错误:
System.ObjectDisposedException:无法访问已处置的对象。 对象名称:“SqlDelegatedTransaction”。在 System.Data.SqlClient.SqlDelegatedTransaction.Rollback(SinglePhaseEnlistment 入伍)在 System.Transactions.TransactionStateDelegatedAborting.EnterState(InternalTransaction tx) 在 System.Transactions.Transaction.Rollback() 在 System.Transactions.TransactionScope.InternalDispose() 在 System.Transactions.TransactionScope.Dispose()
当 TransactionScope 超出范围并且尚未在范围内调用 Complete() 时会发生错误。预期的行为将是事务以静默方式回滚。事务没有提交,所以我们不会在数据库中得到任何损坏的数据。另一方面,我还可以提到我们正在使用 nhibernate。程序流程如下:
using (var transaction = new TransactionScope())
{
using (var session = _sessionManager.OpenSession())
{
// we have to wrap the invocation with an nhibernate transaction due to a dtc bug: http://www.mail-archive.com/nhibernate-development@googlegroups.com/msg02306.html
using (ITransaction nhibernateTrans = session.Session.BeginTransaction())
{
// code altering session data goes here
nhibernateTrans.Commit();
}
}
transaction.Complete();
}
这可能在几个月内发生了一两次,因此我们不会始终如一地看到这种情况,一旦发生,我们就无法重现它。我们可以对服务执行具有相同值的相同命令,它会按预期工作。
【问题讨论】:
-
此处显示的代码无法编译,您在同一范围内有两个变量事务。
-
它只是放在记事本中的示例代码,我可以为你重命名:-)
-
NHibernate 中系统事务处理的返工正在进行中。不幸的是,您无法可靠地重现故障并提供测试用例,因为您的错误用例似乎与任何已知问题都不匹配。如果 NHibernate 对此负有一些责任,也许这次返工将无法解决它,因为缺乏可测试的案例。关于范围处置后仍会发生什么,您可能有兴趣阅读this。关于与 NHibernate 事务混合,最好删除它们并调用
session.Flush()。
标签: c# nhibernate transactionscope distributed-transactions