{@link org.springframework.transaction.PlatformTransactionManager}implementation for a single Hibernate {@link org.hibernate.SessionFactory}. Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread-bound Session per factory. {@link SessionFactoryUtils}and {@link HibernateTemplate} are aware of thread-bound Sessions and participatein such transactions automatically. Using either of those or going through
SessionFactory.getCurrentSession()
is required for Hibernate access code that needs to support this transaction handling mechanism.
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 {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}(i.e. {@link org.springframework.jdbc.datasource.DataSourceUtils#getConnection}or going through a {@link 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 ( {@link #setDataSource}). The given DataSource should obviously match the one used by the given SessionFactory. To achieve this, configure both to the same JNDI DataSource, or preferably create the SessionFactory with {@link LocalSessionFactoryBean} anda local DataSource (which will be autodetected by this transaction manager).
JTA (usually through {@link 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). Normally, JTA setup for Hibernate is somewhat container-specific due to the JTA TransactionManager lookup, required for proper transactional handling of the SessionFactory-level read-write cache.
Fortunately, there is an easier way with Spring: {@link SessionFactoryUtils}(and thus {@link HibernateTemplate}) registers synchronizations with Spring's {@link org.springframework.transaction.support.TransactionSynchronizationManager}(as used by {@link org.springframework.transaction.jta.JtaTransactionManager}), for proper after-completion callbacks. Therefore, as long as Spring's JtaTransactionManager drives the JTA transactions, Hibernate does not require any special configuration for proper JTA participation. Note that there are special restrictions with EJB CMT and restrictive JTA subsystems: See {@link org.springframework.transaction.jta.JtaTransactionManager}'s javadoc for details.
On JDBC 3.0, this transaction manager supports nested transactions via JDBC 3.0 Savepoints. The {@link #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 objects. 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.
Requires Hibernate 3.1 or later, as of Spring 2.5.
@author Juergen Hoeller
@since 1.2
@see #setSessionFactory
@see #setDataSource
@see LocalSessionFactoryBean
@see SessionFactoryUtils#getSession
@see SessionFactoryUtils#applyTransactionTimeout
@see SessionFactoryUtils#releaseSession
@see HibernateTemplate
@see org.hibernate.SessionFactory#getCurrentSession()
@see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
@see org.springframework.jdbc.datasource.DataSourceUtils#applyTransactionTimeout
@see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
@see org.springframework.jdbc.core.JdbcTemplate
@see org.springframework.jdbc.datasource.DataSourceTransactionManager
@see org.springframework.transaction.jta.JtaTransactionManager