Class HibernateTransactionManager

  • All Implemented Interfaces:
    Serializable, org.springframework.beans.factory.InitializingBean, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.support.ResourceTransactionManager

    public class HibernateTransactionManager
    extends org.springframework.transaction.support.AbstractPlatformTransactionManager
    implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.beans.factory.InitializingBean
    Simplify HibernateTransactionManager in spring-orm bundle. Just add SessionUtils.isEnableThreadBinding() support in doGetTranscation
    Version:
    $Id: HibernateTransactionManager.java Feb 28, 2012 10:32:50 PM chaostone $
    Author:
    chaostone
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

        SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterPropertiesSet()  
      protected org.springframework.dao.DataAccessException convertHibernateAccessException​(org.hibernate.HibernateException ex)
      Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
      protected void doBegin​(Object transaction, org.springframework.transaction.TransactionDefinition definition)  
      protected void doCleanupAfterCompletion​(Object transaction)  
      protected void doCommit​(org.springframework.transaction.support.DefaultTransactionStatus status)  
      protected Object doGetTransaction()  
      protected void doResume​(Object transaction, Object suspendedResources)  
      protected void doRollback​(org.springframework.transaction.support.DefaultTransactionStatus status)  
      protected void doSetRollbackOnly​(org.springframework.transaction.support.DefaultTransactionStatus status)  
      protected Object doSuspend​(Object transaction)  
      protected DataSource getDataSource()
      Return the JDBC DataSource that this instance manages transactions for.
      Object getResourceFactory()  
      org.hibernate.SessionFactory getSessionFactory()
      Return the SessionFactory that this instance should manage transactions for.
      protected boolean isExistingTransaction​(Object transaction)  
      protected boolean isSameConnectionForEntireSession​(org.hibernate.Session session)
      Return whether the given Hibernate Session will always hold the same JDBC Connection.
      void setHibernateManagedSession​(boolean hibernateManagedSession)
      Set whether to operate on a Hibernate-managed Session instead of a Spring-managed Session, that is, whether to obtain the Session through Hibernate's SessionFactory.getCurrentSession() instead of SessionFactory.openSession() (with a Spring TransactionSynchronizationManager check preceding it).
      void setPrepareConnection​(boolean prepareConnection)
      Set whether to prepare the underlying JDBC Connection of a transactional Hibernate Session, that is, whether to apply a transaction-specific isolation level and/or the transaction's read-only flag to the underlying JDBC Connection.
      void setSessionFactory​(org.hibernate.SessionFactory sessionFactory)
      Set the SessionFactory that this instance should manage transactions for.
      • Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

        commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
      • Methods inherited from interface org.springframework.transaction.PlatformTransactionManager

        commit, getTransaction, rollback
    • Constructor Detail

      • HibernateTransactionManager

        public HibernateTransactionManager​(org.hibernate.SessionFactory sessionFactory)
        Create a new HibernateTransactionManager instance.
        Parameters:
        sessionFactory - SessionFactory to manage transactions for
    • Method Detail

      • setSessionFactory

        public void setSessionFactory​(org.hibernate.SessionFactory sessionFactory)
        Set the SessionFactory that this instance should manage transactions for.
      • getSessionFactory

        public org.hibernate.SessionFactory getSessionFactory()
        Return the SessionFactory that this instance should manage transactions for.
      • getDataSource

        protected DataSource getDataSource()
        Return the JDBC DataSource that this instance manages transactions for.
      • setPrepareConnection

        public void setPrepareConnection​(boolean prepareConnection)
        Set whether to prepare the underlying JDBC Connection of a transactional Hibernate Session, that is, whether to apply a transaction-specific isolation level and/or the transaction's read-only flag to the underlying JDBC Connection.

        Default is "true". If you turn this flag off, the transaction manager will not support per-transaction isolation levels anymore. It will not call Connection.setReadOnly(true) for read-only transactions anymore either. If this flag is turned off, no cleanup of a JDBC Connection is required after a transaction, since no Connection settings will get modified.

        See Also:
        Connection.setTransactionIsolation(int), Connection.setReadOnly(boolean)
      • setHibernateManagedSession

        public void setHibernateManagedSession​(boolean hibernateManagedSession)
        Set whether to operate on a Hibernate-managed Session instead of a Spring-managed Session, that is, whether to obtain the Session through Hibernate's SessionFactory.getCurrentSession() instead of SessionFactory.openSession() (with a Spring TransactionSynchronizationManager check preceding it).

        Default is "false", i.e. using a Spring-managed Session: taking the current thread-bound Session if available (e.g. in an Open-Session-in-View scenario), creating a new Session for the current transaction otherwise.

        Switch this flag to "true" in order to enforce use of a Hibernate-managed Session. Note that this requires SessionFactory.getCurrentSession() to always return a proper Session when called for a Spring-managed transaction; transaction begin will fail if the getCurrentSession() call fails.

        This mode will typically be used in combination with a custom Hibernate org.hibernate.context.CurrentSessionContext implementation that stores Sessions in a place other than Spring's TransactionSynchronizationManager. It may also be used in combination with Spring's Open-Session-in-View support (using Spring's default BeangleSessionContext), in which case it subtly differs from the Spring-managed Session mode: The pre-bound Session will not receive a clear() call (on rollback) or a disconnect() call (on transaction completion) in such a scenario; this is rather left up to a custom CurrentSessionContext implementation (if desired).

      • afterPropertiesSet

        public void afterPropertiesSet()
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      • getResourceFactory

        public Object getResourceFactory()
        Specified by:
        getResourceFactory in interface org.springframework.transaction.support.ResourceTransactionManager
      • doGetTransaction

        protected Object doGetTransaction()
        Specified by:
        doGetTransaction in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • isExistingTransaction

        protected boolean isExistingTransaction​(Object transaction)
        Overrides:
        isExistingTransaction in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doBegin

        protected void doBegin​(Object transaction,
                               org.springframework.transaction.TransactionDefinition definition)
        Specified by:
        doBegin in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doSuspend

        protected Object doSuspend​(Object transaction)
        Overrides:
        doSuspend in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doResume

        protected void doResume​(Object transaction,
                                Object suspendedResources)
        Overrides:
        doResume in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doCommit

        protected void doCommit​(org.springframework.transaction.support.DefaultTransactionStatus status)
        Specified by:
        doCommit in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doRollback

        protected void doRollback​(org.springframework.transaction.support.DefaultTransactionStatus status)
        Specified by:
        doRollback in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doSetRollbackOnly

        protected void doSetRollbackOnly​(org.springframework.transaction.support.DefaultTransactionStatus status)
        Overrides:
        doSetRollbackOnly in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • doCleanupAfterCompletion

        protected void doCleanupAfterCompletion​(Object transaction)
        Overrides:
        doCleanupAfterCompletion in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      • isSameConnectionForEntireSession

        protected boolean isSameConnectionForEntireSession​(org.hibernate.Session session)
        Return whether the given Hibernate Session will always hold the same JDBC Connection. This is used to check whether the transaction manager can safely prepare and clean up the JDBC Connection used for a transaction.

        Default implementation checks the Session's connection release mode to be "on_close". Unfortunately, this requires casting to SessionImpl, as of Hibernate 3.1. If that cast doesn't work, we'll simply assume we're safe and return true.

        Parameters:
        session - the Hibernate Session to check
        See Also:
        org.hibernate.impl.SessionImpl#getConnectionReleaseMode(), ConnectionReleaseMode.ON_CLOSE
      • convertHibernateAccessException

        protected org.springframework.dao.DataAccessException convertHibernateAccessException​(org.hibernate.HibernateException ex)
        Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

        Will automatically apply a specified SQLExceptionTranslator to a Hibernate JDBCException, else rely on Hibernate's default translation.

        Parameters:
        ex - HibernateException that occured
        Returns:
        a corresponding DataAccessException