Distributed Transaction Failure – Race Condition

I have a process that is using MSDTC (Microsoft Distributed Transaction Co-ordinator) to manage the transaction.

The transaction is controlled inside a c# .Net windows form executable that manages the transaction by the TransactionScope() object.

The transaction is maintained across 2 SQL server and an Oracle server and it all works… most of the time.

All transactions are opened inside a using statement, as follows

using (TransactionScope scope = new TransactionScope())

Or similar.

The overview of the .exe is it monitors a queue of jobs to process. It processes the work inside the transaction successfully commits, and then after that updates the status on the job queue to success or failure.

When there is an application error, the program throws an exception which means it ends the main transaction and in an outer transaction updates the status to failure.

Intermittently when the core logic was throwing errors on many jobs the entire process would fail with the following error:

Connection currently has transaction enlisted.  Finish current transaction and retry.

But it was intermittent. I had the same 12 jobs throwing errors on every run and sometimes it would crash with the above error after 2 jobs, 8 jobs, 4 jobs or it would actually not crash at all. On my development box I could not replicate it at all?

So I figured I had a race condition.

The fix

After the main transaction block of code and before opening the next TransactionScope object to update the status, I put a 100ms sleep.

    // Stop race condition

    item.Success = false; // <-- this is my object for updating the success or failure
    using (TransactionScope scope = new TransactionScope())

The problem has disappeared. I think the re-opening of a new TransactionScope() object did not allow enough time for MSDTC to close the previous transaction and it was crashing.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.