Monday, 8 February 2016

NHibernate & System.Transactions.TransactionException : The operation is not valid for the state of the transaction.


Problem

I haven't come across this in a while as my environments have been quite stable over the last couple of years. Then we got a brand spanking new clean Team City environment and I migrated our build/test/package tasks across, and suddenly half the test fail.

I started with the usual googles but didn't find anything useful. Then a light bulb went on, and I remembered I added something about this to my personal wiki. Maybe someone will stumble upon it and find it helpful.

Solution 


  1. Remote into the Server where your MSSQL server is running.
  2. Open Component Services. (click Start . In the search box, type dcomcnfg , and then press ENTER)
  3. In the Component Services snap-in, double-click Computers , Computer Name , and Distributed Transaction Coordinator .
  4. Right-click Local DTC , click Properties , and then click the Security tab.
  5. Under Security Settings , select the Network DTC Access check box.
  6. Under Transaction Manager Communication , select the Allow Inbound and Allow Outbound check boxes.Select an authentication level for DTC communications. Mutual Authentication Required is the most secure level. 
  7. Click Enable SNA LU 6.2 Transactions to deselect it.
  8. Continue to configure security settings, or click OK .




Full Error

System.Exception : Exception in constructor: System.Transactions.TransactionException: The operation is not valid for the state of the transaction.
   at System.Transactions.TransactionState.EnlistVolatile(InternalTransaction tx, IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistVolatile(IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions)
   at NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus()
   at NHibernate.Impl.SessionImpl..ctor(IDbConnection connection, SessionFactoryImpl factory, Boolean autoclose, Int64 timestamp, IInterceptor interceptor, EntityMode entityMode, Boolean flushBeforeCompletionEnabled, Boolean autoCloseSessionEnabled, ConnectionReleaseMode connectionReleaseMode)
   at NHibernate.Impl.SessionFactoryImpl.OpenSession(IDbConnection connection, Boolean autoClose, Int64 timestamp, IInterceptor sessionLocalInterceptor)
   at NHibernate.Impl.SessionFactoryImpl.OpenSession(IInterceptor sessionLocalInterceptor)