If your persistence layer runs in an application server (e.g. 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. So, 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, you will have to 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 done in session bean deployment descriptors, not programatically, hence, the code is reduced to:
// CMT idiom
Session sess = factory.getCurrentSession();
// do some work
...
In a CMT/EJB even rollback happens automatically, since an unhandled RuntimeException
thrown by a session bean method tells the container to set the global transaction to rollback.
This means 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.
Note that you should choose org.hibernate.transaction.JTATransactionFactory
if you use JTA directly (BMT), and org.hibernate.transaction.CMTTransactionFactory
in a CMT session bean, when you configure Hibernate's transaction factory. Remember to also set hibernate.transaction.manager_lookup_class
. Furthermore, make sure that your hibernate.current_session_context_class
is either unset (backwards compatiblity), or 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 silly 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)
explicity from a finally
block. (Of course, most applications can easily avoid using scroll()
or iterate()
at all from the JTA or CMT code.)