Top Description Inners Fields Constructors Methods
org.springframework.orm.hibernate5

public Class HibernateTransactionManager

extends AbstractPlatformTransactionManager
implements ResourceTransactionManager, BeanFactoryAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.transaction.support.ResourceTransactionManager, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.TransactionManager
Annotations
@SuppressWarnings:serial
Imports
java.sql.Connection, .ResultSet, java.util.function.Consumer, javax.sql.DataSource, jakarta.persistence.PersistenceException, org.hibernate.ConnectionReleaseMode, .FlushMode, .HibernateException, .Interceptor, .Session, .SessionFactory, .Transaction, org.hibernate.engine.spi.SessionImplementor, org.hibernate.resource.transaction.spi.TransactionStatus, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .InitializingBean, org.springframework.dao.DataAccessException, .DataAccessResourceFailureException, org.springframework.jdbc.datasource.ConnectionHolder, .DataSourceUtils, .JdbcTransactionObjectSupport, .TransactionAwareDataSourceProxy, org.springframework.lang.Nullable, org.springframework.transaction.CannotCreateTransactionException, .IllegalTransactionStateException, .InvalidIsolationLevelException, .TransactionDefinition, .TransactionSystemException, org.springframework.transaction.support.AbstractPlatformTransactionManager, .DefaultTransactionStatus, .ResourceTransactionManager, .TransactionSynchronizationManager, org.springframework.util.Assert

org.springframework.transaction.PlatformTransactionManager implementation for a single Hibernate SessionFactory. Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread-bound Session per factory. SessionFactory.getCurrentSession() is required for Hibernate access code that needs to support this transaction handling mechanism, with the SessionFactory being configured with SpringSessionContext.

Supports custom isolation levels, and timeouts that get applied as Hibernate transaction timeouts.

This transaction manager is appropriate for applications that use a single Hibernate SessionFactory for transactional data access, but it also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services which access Hibernate and services which use plain JDBC (without being aware of Hibernate)! Application code needs to stick to the same simple Connection lookup pattern as with org.springframework.jdbc.datasource.DataSourceTransactionManager (i.e. org.springframework.jdbc.datasource.DataSourceUtils#getConnection or going through a org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy).

Note

To be able to register a DataSource's Connection for plain JDBC code, this instance needs to be aware of the DataSource (setDataSource). The given DataSource should obviously match the one used by the given SessionFactory.

JTA (usually through org.springframework.transaction.jta.JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. The DataSource that Hibernate uses needs to be JTA-enabled in such a scenario (see container setup).

This transaction manager supports nested transactions via JDBC Savepoints. The setNestedTransactionAllowed "nestedTransactionAllowed"} flag defaults to "false", though, as nested transactions will just apply to the JDBC Connection, not to the Hibernate Session and its cached entity objects and related context. You can manually set the flag to "true" if you want to use nested transactions for JDBC access code which participates in Hibernate transactions (provided that your JDBC driver supports Savepoints). Note that Hibernate itself does not support nested transactions! Hence, do not expect Hibernate access code to semantically participate in a nested transaction.

Note

Hibernate ORM 6.x is officially only supported as a JPA provider. Please use org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean with org.springframework.orm.jpa.JpaTransactionManager there instead.

Author
Juergen Hoeller
Since
4.2
See Also
setSessionFactory, SessionFactory#getCurrentSession(), org.springframework.jdbc.core.JdbcTemplate, org.springframework.jdbc.support.JdbcTransactionManager, org.springframework.orm.jpa.JpaTransactionManager, org.springframework.orm.jpa.vendor.HibernateJpaDialect

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
HibernateTransactionManager.HibernateTransactionObject

Hibernate transaction object, representing a SessionHolder.

private static class

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private BeanFactory
beanFactory

Just needed for entityInterceptorBeanName.

private DataSource
private Object
private boolean
private boolean
private SessionFactory
sessionFactory

References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.
private Consumer<Session>
Inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager:
loggerSYNCHRONIZATION_ALWAYSSYNCHRONIZATION_NEVERSYNCHRONIZATION_ON_ACTUAL_TRANSACTION

Constructor Summary

AccessConstructor and Description
public
HibernateTransactionManager()

Create a new HibernateTransactionManager instance.

public
HibernateTransactionManager(SessionFactory
the SessionFactory to manage transactions for
sessionFactory
)
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Create a new HibernateTransactionManager instance.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

protected DataAccessException

Returns:

a corresponding DataAccessException
convertHibernateAccessException
(HibernateException
the HibernateException that occurred
ex
)

Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

protected void
disconnectOnCompletion(Session
the Hibernate Session to disconnect
session
)
References Deprecated Session is deprecated or references (maybe indirectly) at least one deprecated element.

Disconnect a pre-existing Hibernate Session on transaction completion, returning its database connection but preserving its entity state.

protected void
doBegin(Object
the transaction object returned by doGetTransaction
transaction
,
TransactionDefinition
a TransactionDefinition instance, describing propagation behavior, isolation level, read-only flag, timeout, and transaction name
definition
)

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doBegin.

Begin a new transaction with semantics according to the given transaction definition.

protected void
doCleanupAfterCompletion(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doCleanupAfterCompletion.

Cleanup resources after transaction completion.

protected void
doCommit(DefaultTransactionStatus
the status representation of the transaction
status
)

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doCommit.

Perform an actual commit of the given transaction.

protected Object
doGetTransaction()

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doGetTransaction.

Return a transaction object for the current transaction state.

protected void
doResume(Object
the transaction object returned by doGetTransaction
transaction
,
Object
the object that holds suspended resources, as returned by doSuspend
suspendedResources
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doResume.

Resume the resources of the current transaction.

protected void
doRollback(DefaultTransactionStatus
the status representation of the transaction
status
)

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doRollback.

Perform an actual rollback of the given transaction.

protected void
doSetRollbackOnly(DefaultTransactionStatus
the status representation of the transaction
status
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doSetRollbackOnly.

Set the given transaction rollback-only.

protected Object
doSuspend(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doSuspend.

Suspend the resources of the current transaction.

public DataSource
getDataSource()

Return the JDBC DataSource that this instance manages transactions for.

public Interceptor
getEntityInterceptor()

Return the current Hibernate entity interceptor, or null if none.

public Object
getResourceFactory()

Implements org.springframework.transaction.support.ResourceTransactionManager.getResourceFactory.

Return the resource factory that this transaction manager operates on, e.g. a JDBC DataSource or a JMS ConnectionFactory.

public SessionFactory
getSessionFactory()
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Return the SessionFactory that this instance should manage transactions for.

protected boolean
isExistingTransaction(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.isExistingTransaction.

Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started).

protected final SessionFactory

Returns:

the SessionFactory (never null)
obtainSessionFactory
()
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Obtain the SessionFactory for actual use.

public void
setAllowResultAccessAfterCompletion(boolean allowResultAccessAfterCompletion)
Deprecated since 5.3.29. as of 5.3.29 since Hibernate 5.x aggressively closes ResultSets on commit, making it impossible to rely on ResultSet holdability.

Set whether to allow result access after completion, typically via Hibernate's ScrollableResults mechanism.

public void
setAutodetectDataSource(boolean autodetectDataSource)

Set whether to autodetect a JDBC DataSource used by the Hibernate SessionFactory, if set via LocalSessionFactoryBean's setDataSource.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

The bean factory just needs to be known for resolving entity interceptor bean names.

public void
setDataSource(DataSource dataSource)

Set the JDBC DataSource that this instance should manage transactions for.

public void
setEntityInterceptor(Interceptor entityInterceptor)

Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.

public void
setEntityInterceptorBeanName(String
the name of the entity interceptor in the bean factory
entityInterceptorBeanName
)

Set the bean name of a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.

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).

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.

public void
setSessionFactory(SessionFactory sessionFactory)
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Set the SessionFactory that this instance should manage transactions for.

public void
setSessionInitializer(Consumer<Session> sessionInitializer)

Specify a callback for customizing every Hibernate Session resource created for a new transaction managed by this HibernateTransactionManager.

Inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager:
commitdetermineTimeoutgetDefaultTimeoutgetTransactiongetTransactionExecutionListenersgetTransactionSynchronizationinvokeAfterCompletionisFailEarlyOnGlobalRollbackOnlyisGlobalRollbackOnParticipationFailureisNestedTransactionAllowedisRollbackOnCommitFailureisValidateExistingTransactionprepareForCommitprepareSynchronizationregisterAfterCompletionWithExistingTransactionresumerollbacksetDefaultTimeoutsetFailEarlyOnGlobalRollbackOnlysetGlobalRollbackOnParticipationFailuresetNestedTransactionAllowedsetRollbackOnCommitFailuresetTransactionExecutionListenerssetTransactionSynchronizationsetTransactionSynchronizationNamesetValidateExistingTransactionshouldCommitOnGlobalRollbackOnlysuspendtriggerBeforeCommittriggerBeforeCompletionuseSavepointForNestedTransaction