Home > c# > Distributed Transaction Failure – Race Condition

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:

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
    System.Threading.Thread.Sleep(100);

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

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.

Cheers
John

VN:F [1.9.22_1171]
Rating: 1.0/10 (1 vote cast)
Distributed Transaction Failure - Race Condition, 1.0 out of 10 based on 1 rating
Categories: c# Tags:
  1. No comments yet.
  1. No trackbacks yet.