Transaction TimeOut EJB impact on the thread

Sandeep Jindal picture Sandeep Jindal · Jul 8, 2011 · Viewed 9.3k times · Source

A question on EJB:

Let's say I have a session bean which has an infinite loop. It is running under a EJB transaction. Now when the transaction of the EJB times out, will that cause the infinite loop thread to break or container will stop the thread running the infinite loop.

Answer

Vineet Reynolds picture Vineet Reynolds · Jul 11, 2011

Now when the transaction of the EJB timesout, will that cause the infinite loop thread to break or container will stop the thread running the infinte loop.

This answer is based on reverse-engineering that I performed a couple of years back on OC4J 10.3.x, WebSphere 6.x and WebLogic 10.x, and might apply to other containers in a similar manner. As far as I remember, the transaction timeout detection is implemented differently in different containers, but they all employ certain common principles stated as follows:

  • The transaction timeout detection is usually performed in a different thread managed by the container. The pertinent thread sleeps for a specified duration (usually 1 second), then wakes up and iterates through all the transactions in progress. If any transaction has exceeded the timeout specified (usually at various levels - the JTA container, the EJB etc.), then the thread will mark the transaction for rollback. No attempt will be made to signal the thread executing the transaction about the transaction state.
  • When the thread performing the transaction attempts to interact with the JTA co-ordinator or with a transactional resource (an XAResource instance) to do some work (for instance, issue a SQL query), the container would determine that the transaction has been marked for a rollback, and would throw a TransactionRolledBackException.

Based on the above, it can be inferred that the infinite loop will never be broken unless a TransactionRolledBackException is thrown. In other words, the loop will be broken only when a transactional activity is attempted within the loop; if no such activity is performed, then the loop will retain it's property to execute indefinitely.

Note that certain containers like WebLogic allow for detection of "stuck" threads. This means that such containers have the ability to detect if a thread has been executing for an extended period of time beyond a configured duration. This done not mean that the container will terminate or interrupt the thread when it detects that one is stuck.