The transaction has aborted. Tricky .NET TransactionScope Behavior.

This Error took me a bunch of time to find. Why? Well, I’m just not smart enough.

The code says everything:

[Test]
public void When_some_transaction_disposes_without_error_or_complete()
{
    // entering an ambient transaction
    new TransactionScope();

    // start a nested transaction
    var t2 = new TransactionScope();
    // no complete, no exception
    // cancels ambient transaction, too
    t2.Dispose();

    // creating a new scope fails
    Action createAScope = () => new TransactionScope();
    createAScope.ShouldThrow<TransactionAbortedException>()
            .Message.ShouldEqual("The transaction has aborted.");
}

In my case, t2 was opened somewhere else only when some conditions were given. To me, the exception seemed to occur randomly.

For sure, after finding the cause the message starts to make sense too. But in the beginning I couldn’t figure out what the error was about.

Hope this post helps someone who consults Google for “TransactionAbortedException” or “The transaction has aborted.”

Advertisements

6 thoughts on “The transaction has aborted. Tricky .NET TransactionScope Behavior.

  1. I am having an issue with this at the moment too, but am a little unclear as to what your solution was (or what you are suggesting is the cause) from the above post?

    Could you possibly help me out with details? I don’t have any issues on my local development machine with it, but when I try to run my code on a server (which definately has DTS turned on), I get the “The transaction has aborted” error.

    I’m using LINQ for my database connections, and I get the error thrown as soon as I try to do anything, such as even just reading out a single record from a single table (on a single connection). No nested transactions, just a single instantiation of a new TransactionScope object.

    Any suggestions would be appreciated!

    Bron

  2. I was having the same issue and thought i would share my fix as it was pretty simple.

    If you have a nested transaction that is throwing an exception the parent transaction will fail. After fixing the nested transaction the parent transaction succeeded.

    HTH

  3. yes that was exactly my problem too I had two exit points from my transaction scope (one after a select and insert, one after only the select) I had not committed the transaction after the select only exit point. Thanks 🙂

  4. I had the same scenario. An Inner TransactionScope which doesn’t close and I try to Commit the upper. Thanks for the GREAT post!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s