PDA

View Full Version : TxScopeTransactionManager.DoRollback


mickeyc
12-12-2006, 09:54 PM
In the DoRollback method of the TxScopeTransactionManager, there is a TODO comment asking if there is another way. Yes there is. To "rollback" the transaction using System.Transactions.TransactionScope, just call Dispose without calling Complete.

In short, replace this

System.Transactions.Transaction.Current.Rollback() ;

with this

txObject.TransactionScope.Dispose();


--Mick

Mark Pollack
12-14-2006, 08:33 PM
Hi Mick,

Thanks! Is there a reason you know of off hand to prefer one over the other? I'll look into it more myself in anycase. If I remember correctly I think my motivation for adding the TODO comment was simply that I didn't like calling the static method, as such, I'll probably end up changing it to your suggestion.

Cheers,
Mark

mickeyc
12-15-2006, 12:45 AM
Not calling Dispose() causes the transaction to hang. Seeing how I'm just a bit impatient, I did not wait to see how long the DTC would take to release the transaction, I just restarted the DTC service. I did take me a bit to figure out that that is what was happening, and after reading a bit of the MSDN docs, I made the change to call Dispose() and it started working.

Typically, scopes would be created with a using statement. In Spring.net's world, that is not possible. So the best we can to is call Dispose() manually, just like the using statement would do for us.

As for using the Current property, I would tend to use it only to gather information about the ambient transaction, but not change anything about it. The idea of the TransactionScope class is "so that the ambient transaction context is automatically managed for you." (from the TransactionScope class help)

--Mick

Mark Pollack
12-15-2006, 09:36 PM
Hi Mick,

Thanks, I've made the change. It seems obvious in retropsect.

Mark