问题
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.
回答1:
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 aTransactionRolledBackException
.
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.
回答2:
No, it is not possible in general for a container to automatically detect an infinite loop. Some application servers might detect that the transaction has timed out or that the EJB has been active for a long time.
来源:https://stackoverflow.com/questions/6623278/transaction-timeout-ejb-impact-on-the-thread