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), especially distributed transaction handling across several datasources. You can also demarcate transaction boundaries programmatically (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 start up.
- JTA Session binding: the Hibernate
Sessioncan be automatically bound to the scope of JTA transactions. Simply lookup the
SessionFactoryfrom JNDI and get the current
Session. Let Hibernate manage 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 Enterprise Application Platform ), you can choose to deploy Hibernate as a managed MBean. This saves you the one line start up code to build your
Configuration. The container will start up your
HibernateServiceand 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 can 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 transactions if an existing transaction is underway in this context (for example, EJB session bean method). Otherwise, a new transaction is started and bean-managed transactions are used.
- delegates to container-managed JTA transactions
TransactionManagerin a managed environment. In an application server, since J2EE does not standardize a single mechanism, you have to specify how Hibernate should obtain a reference to the
Table 3.10. JTA TransactionManagers
|Transaction Factory||Application Server|
| ||WebSphere 6|
| ||Borland ES|