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.
(i.e. org.
or going through a
org.).
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.)
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.
with org. there instead.
setSessionFactory, SessionFactory#getCurrentSession(), org.springframework.jdbc.core.JdbcTemplate , org.springframework.jdbc.support.JdbcTransactionManager , org.springframework.orm.jpa.JpaTransactionManager , org.springframework.orm.jpa.vendor.HibernateJpaDialect
| Modifier and Type | Class and Description |
|---|---|
| private class | HibernateTransactionManager.
Hibernate transaction object, representing a SessionHolder. |
| private static class | HibernateTransactionManager.
Holder for suspended resources. |
| Modifier and Type | Field 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 |
| Access | Constructor and Description |
|---|---|
| public | |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Invoked by the containing 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 DataAccessExceptionthe HibernateException that occurred ex)Convert the given HibernateException to an appropriate exception
from the |
| 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 transaction, TransactionDefinition doGetTransactiona TransactionDefinition instance, describing propagation
behavior, isolation level, read-only flag, timeout, and transaction name definition)Implements abstract org. Begin a new transaction with semantics according to the given transaction definition. |
| protected void | doCleanupAfterCompletion(Object
the transaction object returned by transaction)doGetTransactionOverrides org. Cleanup resources after transaction completion. |
| protected void | doCommit(DefaultTransactionStatus
the status representation of the transaction status)Implements abstract org. Perform an actual commit of the given transaction. |
| protected Object | doGetTransaction()
Implements abstract org. Return a transaction object for the current transaction state. |
| protected void | doResume(Object
the transaction object returned by transaction, Object doGetTransactionthe object that holds suspended resources,
as returned by doSuspend suspendedResources)Overrides org. Resume the resources of the current transaction. |
| protected void | doRollback(DefaultTransactionStatus
the status representation of the transaction status)Implements abstract org. Perform an actual rollback of the given transaction. |
| protected void | doSetRollbackOnly(DefaultTransactionStatus
the status representation of the transaction status)Overrides org. Set the given transaction rollback-only. |
| protected Object | doSuspend(Object
the transaction object returned by transaction)doGetTransactionOverrides org. Suspend the resources of the current transaction. |
| public DataSource | |
| public Interceptor | |
| public Object | getResourceFactory()
Implements org. 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. Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started). |
| protected final SessionFactory | Returns: the SessionFactory (nevernull)
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 |
| public void | setBeanFactory(BeanFactory
owning BeanFactory (never beanFactory)null).
The bean can immediately call methods on the factory.Implements org. 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 |
| 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 |