3.8. J2EE Application Server integration
- Container-managed datasources: Hibernate can use JDBC connections managed by the container and provided through JNDI. Usually, a JTA compatible
ResourceManagertake care of transaction management (CMT), esp. distributed transaction handling across several datasources. You may of course also demarcate transaction boundaries programatically (BMT) or you might want to use the optional Hibernate
TransactionAPI for this to keep your code portable.
- Automatic JNDI binding: Hibernate can bind its
SessionFactoryto JNDI after startup.
- JTA Session binding: The Hibernate
Sessionmay be automatically bound to the scope of JTA transactions. Simply lookup the
SessionFactoryfrom JNDI and get the current
Session. Let Hibernate take care of flushing and closing the
Sessionwhen your JTA transaction completes. Transaction demarcation is either declarative (CMT) or programmatic (BMT/UserTransaction).
- JMX deployment: If you have a JMX capable application server (e.g. JBoss AS), you can chose to deploy Hibernate as a managed MBean. This saves you the one line startup code to build your
Configuration. The container will startup your
HibernateService, and ideally also take care of service dependencies (Datasource has to be available before Hibernate starts, etc).
hibernate.connection.aggressive_releaseto true if your application server shows "connection containment" exceptions.
3.8.1. Transaction strategy configuration
SessionAPI is independent of any transaction demarcation system in your architecture. If you let Hibernate use JDBC directly, through a connection pool, you may begin and end your transactions by calling the JDBC API. If you run in a J2EE application server, you might want to use bean-managed transactions and call the JTA API and
TransactionAPI, which wraps and hides the underlying system. You have to specify a factory class for
Transactioninstances by setting the Hibernate configuration property
- delegates to database (JDBC) transactions (default)
- delegates to container-managed transaction if an existing transaction is underway in this context (e.g. EJB session bean method), otherwise a new transaction is started and bean-managed transaction are used.
- delegates to container-managed JTA transactions
TransactionManagerin a managed environment. In an application server you have to specify how Hibernate should obtain a reference to the
TransactionManager, since J2EE does not standardize a single mechanism:
Table 3.10. JTA TransactionManagers
|Transaction Factory||Application Server|
| ||WebSphere 6|
| ||Borland ES|