3.8. J2EE Application Server integration
3.8.1. Transaction strategy configuration
The Hibernate Session
API 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 UserTransaction
when needed.
To keep your code portable between these two (and other) environments we recommend the optional Hibernate Transaction
API, which wraps and hides the underlying system. You have to specify a factory class for Transaction
instances by setting the Hibernate configuration property hibernate.transaction.factory_class
.
There are three standard (built-in) choices:
-
org.hibernate.transaction.JDBCTransactionFactory
-
delegates to database (JDBC) transactions (default)
-
org.hibernate.transaction.JTATransactionFactory
-
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.
-
org.hibernate.transaction.CMTTransactionFactory
-
delegates to container-managed JTA transactions
You may also define your own transaction strategies (for a CORBA transaction service, for example).
Some features in Hibernate (i.e. the second level cache, Contextual Sessions with JTA, etc.) require access to the JTA TransactionManager
in 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:
Transaction Factory
|
Application Server
|
org.hibernate.transaction.JBossTransactionManagerLookup
|
JBoss
|
org.hibernate.transaction.WeblogicTransactionManagerLookup
|
Weblogic
|
org.hibernate.transaction.WebSphereTransactionManagerLookup
|
WebSphere
|
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
|
WebSphere 6
|
org.hibernate.transaction.OrionTransactionManagerLookup
|
Orion
|
org.hibernate.transaction.ResinTransactionManagerLookup
|
Resin
|
org.hibernate.transaction.JOTMTransactionManagerLookup
|
JOTM
|
org.hibernate.transaction.JOnASTransactionManagerLookup
|
JOnAS
|
org.hibernate.transaction.JRun4TransactionManagerLookup
|
JRun4
|
org.hibernate.transaction.BESTransactionManagerLookup
|
Borland ES
|
Table 3.10. JTA TransactionManagers