Monday, October 26, 2009

Cannot start a new UOW. A LocalTransactionContainment is already active with work

I found out recently that one of the most common search terms used by people who end up at this blog is:
"Cannot start a new UOW. A LocalTransactionContainment is already active with work."

I thought it might be a good idea to write here about why you could come across this message and what you can do about it.

The most likely place you'd see this is in the text of an IllegalStateException thrown from UserTransaction.begin().

Let's have a look at the message and see what it could mean.

The UOW mentioned in the first part of the message is the unit of work you're trying to begin. In the case where the exception is thrown from UserTransaction.begin(), this is saying that it has not been possible to start the UserTransaction.

The second part of the message is the reason the UOW could not be started. LocalTransactionContainment (LTC) is the mechanism used in WebSphere Application Server to handle the unspecified transaction context defined in the EJB specification. You can read about LTC in the WebSphere infocenter here.

In a nutshell, LTC provides a way for an application deployer to specify how transacted work can be automatically completed when it's performed outside of a global transaction. For example LTC can be used to automatically commit all the work done on JDBC connections used outside of a global transaction. The important thing to bear in mind is that pretty much whenever there is no global transaction context on a thread, WebSphere Application Server will have established an LTC.

Our error message is saying that some transacted work (e.g. using a JDBC connection) has been performed and has not yet been committed or rolled back at the time that UserTransaction.begin() is called. The LTC established before UserTransaction.begin() is called is said to be active with work under these circumstances and it is illegal to start a new UOW until that work is completed.

Assuming the intention really is to have the work done outside of the global transaction, the solution to the problem is to explicity commit or roll back the transacted work before the call to UserTransaction.begin().

1 comments:

jay said...

Hi,

I noticed that LTC works differently
in WAS 7.x compared to WAS 6.1.x.
In WAS 6.1.x it looks like a commit()
is implicitly done, while in WAS 7.x
I get the infamous IllegalStateException. The exact error message is :
"java.lang.IllegalStateException: Cannot start a new UOW. A LocalTransactionContainment is already active with work."

Has anyone noticed this ?

cheers,
jay