Transaction timeout not interrupting thread in JBoss EAP
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 4
- 5
- 6
- 7
Issue
A hung transaction did not timeout after 5 minutes (the default value for transaction timeouts).
Resolution
JBoss cannot/will not interrupt a thread that is executing code in the context of a transaction.
If WARN logging is enabled for the transaction components, a warning will be logged as detailed in this arcticle.
For JBoss EAP 5 and later, it may be helpful to add <set-tx-query-timeout>true</set-tx-query-timeout> to the JDBC datasource (configured in *-ds.xml or in EAP 6 and later within the standalone*.xml or domain.xml). This will ensure that Content from java.sun.com is not included.Statement#setQueryTimeout() is invoked with the time remaining in the parent JTA transaction. If the driver honors the timeout1 then it will interrupt the long-running query when the transaction times out. Please note that <set-tx-query-timeout> will have no impact in JBoss EAP 4.
Query timeout is a suggestion or hint to the driver/database which may not be honored by all database driver/server combinations.
Root Cause
When a transaction times out the transaction manager does not interrupt threads operating in the context of the transaction. The transaction manager simply marks the transaction as aborted so that when the thread attempts to enlist new connections, commit the transaction, etc. an exception will be thrown. This allows a thread working in the context of the transaction to perform appropriate cleanup (release resources, rollback, etc.) before terminating its work.
Interrupting a thread can be dangerous. At worst, a resource manager could be confused and left in an inconsistent state by interruption/intervention on the thread. Further, most JDBC drivers use blocking I/O and happily continue on with their work even if the thread has been interrupted. A well behaved resource manager (database, etc.) will eventually time out on its own (this is a completely separate timeout from the JTA timeout set by the transaction manager or application).
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.