com.interface21.orm.hibernate
Class HibernateTransactionManager

java.lang.Object
  |
  +--com.interface21.transaction.support.AbstractPlatformTransactionManager
        |
        +--com.interface21.orm.hibernate.HibernateTransactionManager
All Implemented Interfaces:
InitializingBean, PlatformTransactionManager

public class HibernateTransactionManager
extends AbstractPlatformTransactionManager
implements InitializingBean

PlatformTransactionManager implementation for single Hibernate session factories. Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread Session per factory. SessionFactoryUtils and HibernateTemplate are aware of thread-bound Sessions and take part in such transactions automatically. Using either is required for proper Hibernate access code supporting this transaction handling mechanism. Supports custom isolation levels but not timeouts.

This implementation is appropriate for applications that solely use Hibernate for transactional data access, but it also supports direct data source access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services that access Hibernate including proper transactional caching, and services that use plain JDBC without being aware of Hibernate! To be able to register the Connection for plain JDBC code, the instance needs to be aware of the DataSource (see setDataSource). Application code needs to stick to the same Connection lookup pattern as with DataSourceTransactionManager (i.e. DataSourceUtils.getConnection).

JTA resp. JtaTransactionManager is necessary for accessing multiple transactional resources. The DataSource that Hibernate uses needs to be JTA-enabled then (see container setup), alternatively the Hibernate JCA connector can to be used for direct container integration. Normally, Hibernate JTA setup is somewhat container-specific due to the JTA TransactionManager lookup, required for proper transactional handling of the JVM-level cache. Using the JCA Connector can solve this but involves classloading issues and container-specific connector deployment. Fortunately, there is an easier way with Spring: SessionFactoryUtils' close and thus HibernateTemplate register synchronizations with JtaTransactionManager, for proper completion callbacks. Therefore, as long as JtaTransactionManager demarcates the JTA transactions, Hibernate does not require any special JTA configuration for proper JTA.

Note: This class, like all of Spring's Hibernate support, requires Hibernate 2.0 (initially developed with RC1).

Since:
02.05.2003
Author:
Juergen Hoeller
See Also:
SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean), SessionFactoryUtils.closeSessionIfNecessary(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory), HibernateTemplate.execute(com.interface21.orm.hibernate.HibernateCallback), setDataSource(javax.sql.DataSource), DataSourceTransactionManager, DataSourceUtils.getConnection(javax.sql.DataSource)

Fields inherited from class com.interface21.transaction.support.AbstractPlatformTransactionManager
logger
 
Constructor Summary
HibernateTransactionManager()
          Create a new HibernateTransactionManager instance.
HibernateTransactionManager(net.sf.hibernate.SessionFactory sessionFactory, javax.sql.DataSource dataSource)
          Create a new HibernateTransactionManager instance.
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied.
protected  void doBegin(java.lang.Object transaction, int isolationLevel, int timeout)
          Begin a new transaction with the given isolation level.
protected  void doCommit(TransactionStatus status)
          Perform an actual commit on the given transaction.
protected  java.lang.Object doGetTransaction()
          Return a current transaction object, i.e. a JTA UserTransaction.
protected  void doRollback(TransactionStatus status)
          Perform an actual rollback on the given transaction.
protected  void doSetRollbackOnly(TransactionStatus status)
          Set the given transaction rollback-only.
 javax.sql.DataSource getDataSource()
          Return the J2EE DataSource that this instance manages transactions for.
 net.sf.hibernate.SessionFactory getSessionFactory()
          Return the SessionFactory that this instance should manage transactions for.
protected  boolean isExistingTransaction(java.lang.Object transaction)
          Check if the given transaction object indicates an existing, i.e. already begun, transaction.
 void setDataSource(javax.sql.DataSource dataSource)
          Set the J2EE DataSource that this instance should manage transactions for (i.e. register the Hibernate transaction's JDBC connection to provide it to application code accessing this DataSource).
 void setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
          Set the SessionFactory that this instance should manage transactions for.
 
Methods inherited from class com.interface21.transaction.support.AbstractPlatformTransactionManager
commit, getAllowNonTransactionalExecution, getTransaction, getTransactionSynchronization, rollback, setAllowNonTransactionalExecution, setTransactionSynchronization
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateTransactionManager

public HibernateTransactionManager()
Create a new HibernateTransactionManager instance. A SessionFactory has to be set to be able to use it.
See Also:
setSessionFactory(net.sf.hibernate.SessionFactory)

HibernateTransactionManager

public HibernateTransactionManager(net.sf.hibernate.SessionFactory sessionFactory,
                                   javax.sql.DataSource dataSource)
Create a new HibernateTransactionManager instance.
Parameters:
sessionFactory - SessionFactory to manage transactions for
dataSource - DataSource to manage transactions for
Method Detail

setSessionFactory

public void setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
Set the SessionFactory that this instance should manage transactions for.

getSessionFactory

public net.sf.hibernate.SessionFactory getSessionFactory()
Return the SessionFactory that this instance should manage transactions for.

setDataSource

public final void setDataSource(javax.sql.DataSource dataSource)
Set the J2EE DataSource that this instance should manage transactions for (i.e. register the Hibernate transaction's JDBC connection to provide it to application code accessing this DataSource).

getDataSource

public javax.sql.DataSource getDataSource()
Return the J2EE DataSource that this instance manages transactions for.

afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied.
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
Specified by:
afterPropertiesSet in interface InitializingBean
Following copied from interface: com.interface21.beans.factory.InitializingBean
Throws:
java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

doGetTransaction

protected java.lang.Object doGetTransaction()
                                     throws CannotCreateTransactionException,
                                            TransactionException
Description copied from class: AbstractPlatformTransactionManager
Return a current transaction object, i.e. a JTA UserTransaction.
Overrides:
doGetTransaction in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Returns:
the current transaction object
Throws:
CannotCreateTransactionException - if transaction support is not available (e.g. no JTA UserTransaction retrievable from JNDI)
TransactionException - in case of lookup or system errors

isExistingTransaction

protected boolean isExistingTransaction(java.lang.Object transaction)
                                 throws TransactionException
Description copied from class: AbstractPlatformTransactionManager
Check if the given transaction object indicates an existing, i.e. already begun, transaction.
Overrides:
isExistingTransaction in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
transaction - transaction object returned by doGetTransaction()
Returns:
if there is an existing transaction
Throws:
TransactionException - in case of system errors

doBegin

protected void doBegin(java.lang.Object transaction,
                       int isolationLevel,
                       int timeout)
                throws TransactionException
Description copied from class: AbstractPlatformTransactionManager
Begin a new transaction with the given isolation level.
Overrides:
doBegin in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
transaction - transaction object returned by doGetTransaction()
isolationLevel - desired isolation level
timeout - transaction timeout (in seconds)
Throws:
TransactionException - in case of creation or system errors

doCommit

protected void doCommit(TransactionStatus status)
                 throws TransactionException
Description copied from class: AbstractPlatformTransactionManager
Perform an actual commit on the given transaction. An implementation does not need to check the rollback-only flag.
Overrides:
doCommit in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
status - status representation of the transaction
Throws:
TransactionException - in case of commit or system errors

doRollback

protected void doRollback(TransactionStatus status)
                   throws TransactionException
Description copied from class: AbstractPlatformTransactionManager
Perform an actual rollback on the given transaction. An implementation does not need to check the new transaction flag.
Overrides:
doRollback in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
status - status representation of the transaction
Throws:
TransactionException - in case of system errors

doSetRollbackOnly

protected void doSetRollbackOnly(TransactionStatus status)
                          throws TransactionException
Description copied from class: AbstractPlatformTransactionManager
Set the given transaction rollback-only. Only called on rollback if the current transaction takes part in an existing one.
Overrides:
doSetRollbackOnly in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
status - status representation of the transaction
Throws:
TransactionException - in case of system errors


Rod Johnson and Spring contributors 2001-2003.