J2EE Application Server integration in Hibernate

Hibernate has the following integration points for J2EE infrastructure:

1. Container-managed datasources: Hibernate can use JDBC connections managed by the container and provided through JNDI. Usually, a JTA compatible TransactionManager and a ResourceManager take care of transaction management (CMT), especially distributed transaction handling across several datasources. We can also demarcate transaction boundaries programmatically (BMT), or you might want to use the optional Hibernate Transaction API for this to keep your code portable.

2. Automatic JNDI binding: Hibernate can bind its SessionFactory to JNDI after startup.

3. JTA Session binding: Hibernate Session can be automatically bound to the scope of JTA transactions. Simply lookup the SessionFactory from JNDI and get the current Session. Let Hibernate manage flushing and closing the Session when your JTA transaction completes. Transaction demarcation is either declarative (CMT) or programmatic (BMT/UserTransaction).

4. JMX deployment: JMX capable application server (JBoss AS), can choose to deploy Hibernate as a managed MBean. This saves the one line startup code to build SessionFactory from a Configuration. The container will startup HibernateService and also take care of service dependencies (datasource has to be available before Hibernate starts, etc).

Depending on environment, might have to set the configuration option hibernate.connection.aggressive_release to true if your application server shows "connection containment" exceptions.