0

We are running distributed transactions, and on some rare occasions we get the following error:

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()

The error occurs when the TransactionScope goes out of scope and Complete() has not been called within the scope. The expected behaviour would be that the transaction rolls back silently. The transaction does not commit, so we don't get any corrupt data in the database. As a side not I can also mention that we are using nhibernate. The program flow is as follows:

        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();
        }

This has happened maybe one or two times in a couple of months so we are not seeing this consistently, and once it happens we are unable to reproduce it. We can execute the same command with the same values against the service and it will work as expected.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Marius
  • 9,208
  • 8
  • 50
  • 73
  • The code you show here can't compile, you have two variables transaction within the same scope. – Peter Oct 31 '12 at 10:52
  • Its just example code it put together in notepad, I can rename it for you :-) – Marius Oct 31 '12 at 11:03
  • Rework of system transaction handling in NHibernate is ongoing. It is unfortunate you were not able to reliably reproduce the trouble and supply a test case, because your error case does not seems to match any known issues. If NHibernate has some responsibility in it, maybe this rework will not fix it, lacking a case to test against. About what can still happen after scope disposal, you may be interested in reading [this](https://github.com/npgsql/npgsql/issues/1571#issuecomment-308651461). About mixing with NHibernate transaction, better remove them and call `session.Flush()` instead. – Frédéric Jul 10 '17 at 20:20

1 Answers1

1

There are some threading issues with TransactionScope in NHibernate that haven't been resolved yet. Likely your issues matches one of these: https://nhibernate.jira.com/secure/IssueNavigator.jspa?reset=true&jqlQuery=project+%3D+NH+AND+labels+%3D+TransactionScope

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36