| java.lang.Object org.springframework.transaction.support.AbstractPlatformTransactionManager org.springframework.orm.hibernate.HibernateTransactionManager
HibernateTransactionManager | public class HibernateTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager,BeanFactoryAware,InitializingBean(Code) | | org.springframework.transaction.PlatformTransactionManager implementation for a single Hibernate
net.sf.hibernate.SessionFactory .
Binds a Hibernate Session from the specified factory to the thread, potentially
allowing for one thread-bound Session per factory.
SessionFactoryUtils and
HibernateTemplate are aware of thread-bound Sessions and participate
in such transactions automatically. Using either of those is required for
Hibernate access code that needs to support this transaction handling mechanism.
Supports custom isolation levels, and timeouts that get applied as appropriate
Hibernate query timeouts. To support the latter, application code must either use
HibernateTemplate (which by default applies the timeouts) or call
SessionFactoryUtils.applyTransactionTimeout for each created
Hibernate
net.sf.hibernate.Query object.
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 (
HibernateTransactionManager.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
LocalSessionFactoryBean and
a local DataSource (which will be autodetected by this transaction manager).
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). 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:
SessionFactoryUtils (and thus
HibernateTemplate ) registers synchronizations with Spring's
org.springframework.transaction.support.TransactionSynchronizationManager (as used by
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
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
HibernateTransactionManager.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.
Note: Spring's Hibernate support in this package requires Hibernate 2.1.
Dedicated Hibernate3 support can be found in a separate package:
org.springframework.orm.hibernate3 .
author: Juergen Hoeller since: 02.05.2003 See Also: HibernateTransactionManager.setSessionFactory See Also: HibernateTransactionManager.setDataSource See Also: LocalSessionFactoryBean See Also: SessionFactoryUtils.getSession See Also: SessionFactoryUtils.applyTransactionTimeout See Also: SessionFactoryUtils.releaseSession See Also: HibernateTemplate.execute See Also: org.springframework.jdbc.datasource.DataSourceUtils.getConnection See Also: org.springframework.jdbc.datasource.DataSourceUtils.applyTransactionTimeout See Also: org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection See Also: org.springframework.jdbc.core.JdbcTemplate See Also: org.springframework.jdbc.datasource.DataSourceTransactionManager See Also: org.springframework.transaction.jta.JtaTransactionManager |
HibernateTransactionManager | public HibernateTransactionManager(SessionFactory sessionFactory)(Code) | | Create a new HibernateTransactionManager instance.
Parameters: sessionFactory - SessionFactory to manage transactions for |
afterPropertiesSet | public void afterPropertiesSet()(Code) | | |
convertJdbcAccessException | protected DataAccessException convertJdbcAccessException(JDBCException ex)(Code) | | Convert the given JDBCException to an appropriate exception from the
org.springframework.dao hierarchy.
Uses a JDBC exception translator. Can be overridden in subclasses.
Parameters: ex - JDBCException that occured, wrapping a SQLException the corresponding DataAccessException instance See Also: HibernateTransactionManager.setJdbcExceptionTranslator |
doCleanupAfterCompletion | protected void doCleanupAfterCompletion(Object transaction)(Code) | | |
getDataSource | public DataSource getDataSource()(Code) | | Return the JDBC DataSource that this instance manages transactions for.
|
getSessionFactory | public SessionFactory getSessionFactory()(Code) | | Return the SessionFactory that this instance should manage transactions for.
|
isExistingTransaction | protected boolean isExistingTransaction(Object transaction)(Code) | | |
setAutodetectDataSource | public void setAutodetectDataSource(boolean autodetectDataSource)(Code) | | Set whether to autodetect a JDBC DataSource used by the Hibernate SessionFactory,
if set via LocalSessionFactoryBean's setDataSource . Default is "true".
Can be turned off to deliberately ignore an available DataSource,
to not expose Hibernate transactions as JDBC transactions for that DataSource.
See Also: HibernateTransactionManager.setDataSource See Also: LocalSessionFactoryBean.setDataSource |
setDataSource | public void setDataSource(DataSource dataSource)(Code) | | Set the JDBC DataSource that this instance should manage transactions for.
The DataSource should match the one used by the Hibernate SessionFactory:
for example, you could specify the same JNDI DataSource for both.
If the SessionFactory was configured with LocalDataSourceConnectionProvider,
i.e. by Spring's LocalSessionFactoryBean with a specified "dataSource",
the DataSource will be autodetected: You can still explictly specify the
DataSource, but you don't need to in this case.
A transactional JDBC Connection for this DataSource will be provided to
application code accessing this DataSource directly via DataSourceUtils
or JdbcTemplate. The Connection will be taken from the Hibernate Session.
The DataSource specified here should be the target DataSource to manage
transactions for, not a TransactionAwareDataSourceProxy. Only data access
code may work with TransactionAwareDataSourceProxy, while the transaction
manager needs to work on the underlying target DataSource. If there's
nevertheless a TransactionAwareDataSourceProxy passed in, it will be
unwrapped to extract its target DataSource.
See Also: HibernateTransactionManager.setAutodetectDataSource See Also: LocalDataSourceConnectionProvider See Also: LocalSessionFactoryBean.setDataSource See Also: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy See Also: org.springframework.jdbc.datasource.DataSourceUtils See Also: org.springframework.jdbc.core.JdbcTemplate |
setEntityInterceptor | public void setEntityInterceptor(Interceptor entityInterceptor)(Code) | | Set a Hibernate entity interceptor that allows to inspect and change
property values before writing to and reading from the database.
Will get applied to any new Session created by this transaction manager.
Such an interceptor can either be set at the SessionFactory level,
i.e. on LocalSessionFactoryBean, or at the Session level, i.e. on
HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager.
It's preferable to set it on LocalSessionFactoryBean or HibernateTransactionManager
to avoid repeated configuration and guarantee consistent behavior in transactions.
See Also: LocalSessionFactoryBean.setEntityInterceptor See Also: HibernateTemplate.setEntityInterceptor See Also: HibernateInterceptor.setEntityInterceptor |
setEntityInterceptorBeanName | public void setEntityInterceptorBeanName(String entityInterceptorBeanName)(Code) | | 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.
Will get applied to any new Session created by this transaction manager.
Requires the bean factory to be known, to be able to resolve the bean
name to an interceptor instance on session creation. Typically used for
prototype interceptors, i.e. a new interceptor instance per session.
Can also be used for shared interceptor instances, but it is recommended
to set the interceptor reference directly in such a scenario.
Parameters: entityInterceptorBeanName - the name of the entity interceptor inthe bean factory See Also: HibernateTransactionManager.setBeanFactory See Also: HibernateTransactionManager.setEntityInterceptor |
setSessionFactory | public void setSessionFactory(SessionFactory sessionFactory)(Code) | | Set the SessionFactory that this instance should manage transactions for.
|
Fields inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager | final public static int SYNCHRONIZATION_ALWAYS(Code)(Java Doc) final public static int SYNCHRONIZATION_NEVER(Code)(Java Doc) final public static int SYNCHRONIZATION_ON_ACTUAL_TRANSACTION(Code)(Java Doc) protected transient Log logger(Code)(Java Doc)
|
Methods inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager | final public void commit(TransactionStatus status) throws TransactionException(Code)(Java Doc) protected int determineTimeout(TransactionDefinition definition)(Code)(Java Doc) abstract protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException(Code)(Java Doc) protected void doCleanupAfterCompletion(Object transaction)(Code)(Java Doc) abstract protected void doCommit(DefaultTransactionStatus status) throws TransactionException(Code)(Java Doc) abstract protected Object doGetTransaction() throws TransactionException(Code)(Java Doc) protected void doResume(Object transaction, Object suspendedResources) throws TransactionException(Code)(Java Doc) abstract protected void doRollback(DefaultTransactionStatus status) throws TransactionException(Code)(Java Doc) protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException(Code)(Java Doc) protected Object doSuspend(Object transaction) throws TransactionException(Code)(Java Doc) final public int getDefaultTimeout()(Code)(Java Doc) final public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException(Code)(Java Doc) final public int getTransactionSynchronization()(Code)(Java Doc) final protected void invokeAfterCompletion(List synchronizations, int completionStatus)(Code)(Java Doc) protected boolean isExistingTransaction(Object transaction) throws TransactionException(Code)(Java Doc) final public boolean isFailEarlyOnGlobalRollbackOnly()(Code)(Java Doc) final public boolean isGlobalRollbackOnParticipationFailure()(Code)(Java Doc) final public boolean isNestedTransactionAllowed()(Code)(Java Doc) final public boolean isRollbackOnCommitFailure()(Code)(Java Doc) protected DefaultTransactionStatus newTransactionStatus(TransactionDefinition definition, Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, Object suspendedResources)(Code)(Java Doc) protected void registerAfterCompletionWithExistingTransaction(Object transaction, List synchronizations) throws TransactionException(Code)(Java Doc) final protected void resume(Object transaction, SuspendedResourcesHolder resourcesHolder) throws TransactionException(Code)(Java Doc) final public void rollback(TransactionStatus status) throws TransactionException(Code)(Java Doc) final public void setDefaultTimeout(int defaultTimeout)(Code)(Java Doc) final public void setFailEarlyOnGlobalRollbackOnly(boolean failEarlyOnGlobalRollbackOnly)(Code)(Java Doc) final public void setGlobalRollbackOnParticipationFailure(boolean globalRollbackOnParticipationFailure)(Code)(Java Doc) final public void setNestedTransactionAllowed(boolean nestedTransactionAllowed)(Code)(Java Doc) final public void setRollbackOnCommitFailure(boolean rollbackOnCommitFailure)(Code)(Java Doc) final public void setTransactionSynchronization(int transactionSynchronization)(Code)(Java Doc) final public void setTransactionSynchronizationName(String constantName)(Code)(Java Doc) protected boolean shouldCommitOnGlobalRollbackOnly()(Code)(Java Doc) final protected SuspendedResourcesHolder suspend(Object transaction) throws TransactionException(Code)(Java Doc) final protected void triggerBeforeCommit(DefaultTransactionStatus status)(Code)(Java Doc) final protected void triggerBeforeCompletion(DefaultTransactionStatus status)(Code)(Java Doc) protected boolean useSavepointForNestedTransaction()(Code)(Java Doc)
|
|
|