Class HibernateTransactionManager
- java.lang.Object
-
- org.springframework.transaction.support.AbstractPlatformTransactionManager
-
- org.beangle.orm.hibernate.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.InitializingBeanSimplify 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
-
-
Constructor Summary
Constructors Constructor Description HibernateTransactionManager()Create a new HibernateTransactionManager instance.HibernateTransactionManager(org.hibernate.SessionFactory sessionFactory)Create a new HibernateTransactionManager instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()protected org.springframework.dao.DataAccessExceptionconvertHibernateAccessException(org.hibernate.HibernateException ex)Convert the given HibernateException to an appropriate exception from theorg.springframework.daohierarchy.protected voiddoBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)protected voiddoCleanupAfterCompletion(Object transaction)protected voiddoCommit(org.springframework.transaction.support.DefaultTransactionStatus status)protected ObjectdoGetTransaction()protected voiddoResume(Object transaction, Object suspendedResources)protected voiddoRollback(org.springframework.transaction.support.DefaultTransactionStatus status)protected voiddoSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)protected ObjectdoSuspend(Object transaction)protected DataSourcegetDataSource()Return the JDBC DataSource that this instance manages transactions for.ObjectgetResourceFactory()org.hibernate.SessionFactorygetSessionFactory()Return the SessionFactory that this instance should manage transactions for.protected booleanisExistingTransaction(Object transaction)protected booleanisSameConnectionForEntireSession(org.hibernate.Session session)Return whether the given Hibernate Session will always hold the same JDBC Connection.voidsetHibernateManagedSession(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'sSessionFactory.getCurrentSession()instead ofSessionFactory.openSession()(with a SpringTransactionSynchronizationManagercheck preceding it).voidsetPrepareConnection(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.voidsetSessionFactory(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
-
-
-
-
Constructor Detail
-
HibernateTransactionManager
public HibernateTransactionManager()
Create a new HibernateTransactionManager instance. A SessionFactory has to be set to be able to use it.
-
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.
-
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'sSessionFactory.getCurrentSession()instead ofSessionFactory.openSession()(with a SpringTransactionSynchronizationManagercheck 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 thegetCurrentSession()call fails.This mode will typically be used in combination with a custom Hibernate
org.hibernate.context.CurrentSessionContextimplementation 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 defaultBeangleSessionContext), in which case it subtly differs from the Spring-managed Session mode: The pre-bound Session will not receive aclear()call (on rollback) or adisconnect()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:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
getResourceFactory
public Object getResourceFactory()
- Specified by:
getResourceFactoryin interfaceorg.springframework.transaction.support.ResourceTransactionManager
-
doGetTransaction
protected Object doGetTransaction()
- Specified by:
doGetTransactionin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
isExistingTransaction
protected boolean isExistingTransaction(Object transaction)
- Overrides:
isExistingTransactionin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doBegin
protected void doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)
- Specified by:
doBeginin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doSuspend
protected Object doSuspend(Object transaction)
- Overrides:
doSuspendin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doResume
protected void doResume(Object transaction, Object suspendedResources)
- Overrides:
doResumein classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doCommit
protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
- Specified by:
doCommitin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doRollback
protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)
- Specified by:
doRollbackin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doSetRollbackOnly
protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)
- Overrides:
doSetRollbackOnlyin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doCleanupAfterCompletion
protected void doCleanupAfterCompletion(Object transaction)
- Overrides:
doCleanupAfterCompletionin classorg.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 theorg.springframework.daohierarchy.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
-
-