我們正在運行分佈式事務,並在某些罕見的情況下,我們得到以下錯誤:DTS事務失敗:無法訪問已釋放的對象
System.ObjectDisposedException: Cannot access a disposed object. Object name: 'SqlDelegatedTransaction'. at System.Data.SqlClient.SqlDelegatedTransaction.Rollback(SinglePhaseEnlistment enlistment) at System.Transactions.TransactionStateDelegatedAborting.EnterState(InternalTransaction tx) at System.Transactions.Transaction.Rollback() at System.Transactions.TransactionScope.InternalDispose() at System.Transactions.TransactionScope.Dispose()
時的TransactionScope超出範圍和完成(出現的錯誤)在範圍內未被調用。預期的行爲是交易無聲地回滾。事務沒有提交,所以我們不會在數據庫中得到任何損壞的數據。作爲一個方面,我也可以提到我們使用的是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/[email protected]/msg02306.html
using (ITransaction nhibernateTrans = session.Session.BeginTransaction())
{
// code altering session data goes here
nhibernateTrans.Commit();
}
}
transaction.Complete();
}
這已經發生了,也許一次或兩次在一兩個月的,所以我們沒有看到這種一致,一旦發生了我們無法複製它。我們可以用相同的值對服務執行相同的命令,它將按預期工作。
你在這裏顯示的代碼不能編譯,你有兩個變量在同一個範圍內的事務。 – Peter
它只是示例代碼它放在記事本中,我可以爲它重新命名:-) – Marius
NHibernate中的系統事務處理的返工正在進行中。不幸的是,您無法可靠地重現問題並提供測試用例,因爲您的錯誤案例似乎不符合任何已知問題。如果NHibernate有一些責任,也許這個返工不能解決它,缺乏一個案例來測試。關於範圍處理後仍然會發生什麼,您可能有興趣閱讀[this](https://github.com/npgsql/npgsql/issues/1571#issuecomment-308651461)。關於與NHibernate事務混合,更好地移除它們並改爲調用'session.Flush()'。 –