TransactionScope throwing TransactionAbortedException on dispose, used with log4net

I encountered a problem when using a TransactionScope. For some reason, after leaving the scope of the using block, an exception was being thrown saying the transaction had been aborted. I received the following message : System.Transactions.TransactionAbortedException: The Transaction has been aborted.

Reading the MSDN documentation on TransactionScope reveals this tidbit of information: “If you modify the value of [current: ambient transaction] inside a scope, an exception is thrown when Dispose is called.

In my case, the ambient transaction was being modified by a call to log4net, which had a log4net.Appender.AdoNetAppender configured.

Modifying the connection string to add “;Enlist=false” solved the problem by preventing the connection used by log4net to intervene in my TransactionScope’s transaction.

You can find more information on the Enlist parameter here. Basically, what it does is indicate if the connection is automatically enlisted in the current thread’s current transaction context.

This solution applies to log4net but would also apply in other case where you have a second connection which would get enlisted.

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