11.2.2. Using JTA
If your persistence layer runs in an application server (for example, behind EJB session beans), every datasource connection obtained by Hibernate will automatically be part of the global JTA transaction. You can also install a standalone JTA implementation and use it without EJB. Hibernate offers two strategies for JTA integration.
If you use bean-managed transactions (BMT), Hibernate will tell the application server to start and end a BMT transaction if you use the
Transaction
API. The transaction management code is identical to the non-managed environment.
// BMT idiom Session sess = factory.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); // do some work ... tx.commit(); } catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; // or display error message } finally { sess.close(); }
If you want to use a transaction-bound
Session
, that is, the getCurrentSession()
functionality for easy context propagation, use the JTA UserTransaction
API directly:
// BMT idiom with getCurrentSession() try { UserTransaction tx = (UserTransaction)new InitialContext() .lookup("java:comp/UserTransaction"); tx.begin(); // Do some work on Session bound to transaction factory.getCurrentSession().load(...); factory.getCurrentSession().persist(...); tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; // or display error message }
With CMT, transaction demarcation is completed in session bean deployment descriptors, not programmatically. The code is reduced to:
// CMT idiom Session sess = factory.getCurrentSession(); // do some work ...
In a CMT/EJB, even rollback happens automatically. An unhandled
RuntimeException
thrown by a session bean method tells the container to set the global transaction to rollback. You do not need to use the Hibernate Transaction
API at all with BMT or CMT, and you get automatic propagation of the "current" Session bound to the transaction.
When configuring Hibernate's transaction factory, choose
org.hibernate.transaction.JTATransactionFactory
if you use JTA directly (BMT), and org.hibernate.transaction.CMTTransactionFactory
in a CMT session bean. Remember to also set hibernate.transaction.manager_lookup_class
. Ensure that your hibernate.current_session_context_class
is either unset (backwards compatibility), or is set to "jta"
.
The
getCurrentSession()
operation has one downside in a JTA environment. There is one caveat to the use of after_statement
connection release mode, which is then used by default. Due to a limitation of the JTA spec, it is not possible for Hibernate to automatically clean up any unclosed ScrollableResults
or Iterator
instances returned by scroll()
or iterate()
. You must release the underlying database cursor by calling ScrollableResults.close()
or Hibernate.close(Iterator)
explicitly from a finally
block. Most applications can easily avoid using scroll()
or iterate()
from the JTA or CMT code.)