When trying to debug something I found code that was effectively doing the following:
- Creating a TransactionScope
- Creating a Transaction (in this case an nHibernate tx, but not really important)
- Creating a second transaction (in this case a standard ADO.Net Tx)
- Committing the second transaction
- Calling Complete() on the Transaction scope
- Disposing the Transaction Scope.
Now - Creating a transaction and not committing is probably a bad idea anyway - especially when having (and that was the bug fix).
However when testing this - I tried various combinations of the above (committing all transactions, some transactions, no transactions (i.e. only TScope) committing the First, but not second, adding other transactions etc) and in all thesting I found that the following to be true:
Only when I failed to commit the first transaction AND the transaction scope became distributed, the Dispose of the TScope would fail with:
System.InvalidOperationException : The operation is not valid for the current state of the enlistment.
I am now curious and would like to know why this is the case?