org.springframework.orm.hibernate
Class SessionFactoryUtils

java.lang.Object
  extended byorg.springframework.orm.hibernate.SessionFactoryUtils

public abstract class SessionFactoryUtils
extends Object

Helper class featuring methods for Hibernate session handling, allowing for reuse of Hibernate Session instances within transactions.

Supports synchronization with both Spring-managed JTA transactions (i.e. JtaTransactionManager) and non-Spring JTA transactions (i.e. plain JTA or EJB CMT). See the getSession version with all parameters for details.

Used internally by HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager. Can also be used directly in application code, e.g. in combination with HibernateInterceptor.

Note: Spring's Hibernate support requires Hibernate 2.1 (as of Spring 1.0).

Since:
02.05.2003
Author:
Juergen Hoeller
See Also:
getSession(SessionFactory, Interceptor, SQLExceptionTranslator, boolean), HibernateTemplate, HibernateInterceptor, HibernateTransactionManager, JtaTransactionManager

Field Summary
static int SESSION_SYNCHRONIZATION_ORDER
          Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
 
Constructor Summary
SessionFactoryUtils()
           
 
Method Summary
static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory)
          Apply the current transaction timeout, if any, to the given Hibernate Criteria object.
static void applyTransactionTimeout(Query query, SessionFactory sessionFactory)
          Apply the current transaction timeout, if any, to the given Hibernate Query object.
static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory)
          Close the given Session, created via the given factory, if it isn't bound to the thread.
static DataAccessException convertHibernateAccessException(HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
static DataSource getDataSource(SessionFactory sessionFactory)
          Determine the DataSource of the given SessionFactory.
static TransactionManager getJtaTransactionManager(SessionFactory sessionFactory, Session session)
          Try to retrieve the JTA TransactionManager from the given SessionFactory and/or Session.
static Session getNewSession(SessionFactory sessionFactory)
          Get a new Hibernate Session from the given SessionFactory.
static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor)
          Get a new Hibernate Session from the given SessionFactory.
static Session getSession(SessionFactory sessionFactory, boolean allowCreate)
          Get a Hibernate Session for the given SessionFactory.
static Session getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator)
          Get a Hibernate Session for the given SessionFactory.
static Session getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator, boolean allowSynchronization)
          Get a Hibernate Session for the given SessionFactory.
static void initDeferredClose(SessionFactory sessionFactory)
          Initialize deferred close for the current thread and the given SessionFactory.
static boolean isDeferredCloseActive(SessionFactory sessionFactory)
          Return if deferred close is active for the current thread and the given SessionFactory.
static boolean isSessionTransactional(Session session, SessionFactory sessionFactory)
          Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.
static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory)
          Create an appropriate SQLExceptionTranslator for the given SessionFactory.
static void processDeferredClose(SessionFactory sessionFactory)
          Process Sessions that have been registered for deferred close for the given SessionFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SESSION_SYNCHRONIZATION_ORDER

public static final int SESSION_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up Hibernate Sessions. Return DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100 to execute Session cleanup before JDBC Connection cleanup, if any.

See Also:
DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER, Constant Field Values
Constructor Detail

SessionFactoryUtils

public SessionFactoryUtils()
Method Detail

getDataSource

public static DataSource getDataSource(SessionFactory sessionFactory)
Determine the DataSource of the given SessionFactory.

Parameters:
sessionFactory - the SessionFactory to check
Returns:
the DataSource, or null if none found
See Also:
SessionFactoryImplementor.getConnectionProvider(), LocalDataSourceConnectionProvider

newJdbcExceptionTranslator

public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory)
Create an appropriate SQLExceptionTranslator for the given SessionFactory. If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource is created; else, a SQLStateSQLExceptionTranslator as fallback.

Parameters:
sessionFactory - the SessionFactory to create the translator for
Returns:
the SQLExceptionTranslator
See Also:
getDataSource(net.sf.hibernate.SessionFactory), SQLErrorCodeSQLExceptionTranslator, SQLStateSQLExceptionTranslator

getJtaTransactionManager

public static TransactionManager getJtaTransactionManager(SessionFactory sessionFactory,
                                                          Session session)
Try to retrieve the JTA TransactionManager from the given SessionFactory and/or Session. Check the passed-in SessionFactory for implementing SessionFactoryImplementor (the usual case), falling back to the SessionFactory reference that the Session itself carries (for example, when using Hibernate's JCA Connector, i.e. JCASessionFactoryImpl).

Parameters:
sessionFactory - Hibernate SessionFactory
session - Hibernate Session (can also be null)
Returns:
the JTA TransactionManager, if any
See Also:
TransactionManager, SessionFactoryImplementor.getTransactionManager(), Session.getSessionFactory(), SessionFactoryImpl, JCASessionFactoryImpl

getSession

public static Session getSession(SessionFactory sessionFactory,
                                 boolean allowCreate)
                          throws DataAccessResourceFailureException,
                                 IllegalStateException
Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will create a new Session otherwise, if allowCreate is true.

This is the getSession method used by typical data access code, in combination with closeSessionIfNecessary called when done with the Session. Note that HibernateTemplate allows to write data access code without caring about such resource handling.

Supports synchronization with both Spring-managed JTA transactions (i.e. JtaTransactionManager) and non-Spring JTA transactions (i.e. plain JTA or EJB CMT). See the getSession version with all parameters for details.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
allowCreate - if a new Session should be created if no thread-bound found
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
getSession(SessionFactory, Interceptor, SQLExceptionTranslator, boolean), closeSessionIfNecessary(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory), HibernateTemplate

getSession

public static Session getSession(SessionFactory sessionFactory,
                                 Interceptor entityInterceptor,
                                 SQLExceptionTranslator jdbcExceptionTranslator)
Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will always create a new Session otherwise.

Supports synchronization with both Spring-managed JTA transactions (i.e. JtaTransactionManager) and non-Spring JTA transactions (i.e. plain JTA or EJB CMT). See the full getSession version for details.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
entityInterceptor - Hibernate entity interceptor, or null if none
jdbcExceptionTranslator - SQLExcepionTranslator to use for flushing the Session on transaction synchronization (can be null; only used when actually registering a transaction synchronization)
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
See Also:
getSession(SessionFactory, Interceptor, SQLExceptionTranslator, boolean)

getSession

public static Session getSession(SessionFactory sessionFactory,
                                 Interceptor entityInterceptor,
                                 SQLExceptionTranslator jdbcExceptionTranslator,
                                 boolean allowSynchronization)
                          throws DataAccessResourceFailureException
Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will always create a new Session otherwise.

Supports synchronization with Spring-managed JTA transactions (i.e. JtaTransactionManager) via TransactionSynchronizationManager, to allow for transaction-scoped Hibernate Sessions and proper transactional handling of the JVM-level cache. This will only occur if "allowSynchronization" is true.

Supports synchronization with non-Spring JTA transactions (i.e. plain JTA or EJB CMT) via TransactionSynchronizationManager, to allow for transaction-scoped Hibernate Sessions without JtaTransactionManager. This only applies when a JTA TransactionManagerLookup is specified in the Hibernate configuration, and when "allowSynchronization" is true.

Supports setting a Session-level Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Such an interceptor can also be set at the SessionFactory level (i.e. on LocalSessionFactoryBean), on HibernateTransactionManager, or on HibernateInterceptor/HibernateTemplate.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
entityInterceptor - Hibernate entity interceptor, or null if none
jdbcExceptionTranslator - SQLExcepionTranslator to use for flushing the Session on transaction synchronization (can be null; only used when actually registering a transaction synchronization)
allowSynchronization - if a new Hibernate Session is supposed to be registered with transaction synchronization (if synchronization is active). This will always be true for typical data access code.
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
See Also:
LocalSessionFactoryBean.setEntityInterceptor(net.sf.hibernate.Interceptor), HibernateAccessor.setEntityInterceptor(net.sf.hibernate.Interceptor), HibernateAccessor.setEntityInterceptor(net.sf.hibernate.Interceptor), HibernateTransactionManager, JtaTransactionManager, TransactionSynchronizationManager

getNewSession

public static Session getNewSession(SessionFactory sessionFactory)
Get a new Hibernate Session from the given SessionFactory. Will return a new Session even if there already is a pre-bound Session for the given SessionFactory.

Within a transaction, this method will create a new Session that shares the transaction's JDBC Connection. More specifically, it will use the same JDBC Connection as the pre-bound Hibernate Session.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
Returns:
the new Session

getNewSession

public static Session getNewSession(SessionFactory sessionFactory,
                                    Interceptor entityInterceptor)
Get a new Hibernate Session from the given SessionFactory. Will return a new Session even if there already is a pre-bound Session for the given SessionFactory.

Within a transaction, this method will create a new Session that shares the transaction's JDBC Connection. More specifically, it will use the same JDBC Connection as the pre-bound Hibernate Session.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
entityInterceptor - Hibernate entity interceptor, or null if none
Returns:
the new Session

isSessionTransactional

public static boolean isSessionTransactional(Session session,
                                             SessionFactory sessionFactory)
Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.

Parameters:
session - the Hibernate Session to check
sessionFactory - Hibernate SessionFactory that the Session was created with
Returns:
whether the Session is transactional

applyTransactionTimeout

public static void applyTransactionTimeout(Query query,
                                           SessionFactory sessionFactory)
Apply the current transaction timeout, if any, to the given Hibernate Query object.

Parameters:
query - the Hibernate Query object
sessionFactory - Hibernate SessionFactory that the Query was created for
See Also:
Query.setTimeout(int)

applyTransactionTimeout

public static void applyTransactionTimeout(Criteria criteria,
                                           SessionFactory sessionFactory)
Apply the current transaction timeout, if any, to the given Hibernate Criteria object.

Parameters:
criteria - the Hibernate Criteria object
sessionFactory - Hibernate SessionFactory that the Criteria was created for
See Also:
Criteria.setTimeout(int)

convertHibernateAccessException

public static DataAccessException convertHibernateAccessException(HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy. Note that it is advisable to handle JDBCException specifically by using an SQLExceptionTranslator for the underlying SQLException.

Parameters:
ex - HibernateException that occured
Returns:
the corresponding DataAccessException instance
See Also:
HibernateAccessor.convertHibernateAccessException(net.sf.hibernate.HibernateException), HibernateAccessor.convertJdbcAccessException(java.sql.SQLException), HibernateTransactionManager.convertHibernateAccessException(net.sf.hibernate.HibernateException), HibernateTransactionManager.convertJdbcAccessException(java.sql.SQLException), JDBCException.getSQLException(), SQLExceptionTranslator

isDeferredCloseActive

public static boolean isDeferredCloseActive(SessionFactory sessionFactory)
Return if deferred close is active for the current thread and the given SessionFactory.

Parameters:
sessionFactory - the Hibernate SessionFactory to check

initDeferredClose

public static void initDeferredClose(SessionFactory sessionFactory)
Initialize deferred close for the current thread and the given SessionFactory. Sessions will not be actually closed on close calls then, but rather at a processDeferredClose call at a finishing point (like request completion).

Used by OpenSessionInViewFilter and OpenSessionInViewInterceptor when not configured for a single session.

Parameters:
sessionFactory - Hibernate SessionFactory
See Also:
processDeferredClose(net.sf.hibernate.SessionFactory), closeSessionIfNecessary(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory), OpenSessionInViewFilter.setSingleSession(boolean), OpenSessionInViewInterceptor.setSingleSession(boolean)

processDeferredClose

public static void processDeferredClose(SessionFactory sessionFactory)
Process Sessions that have been registered for deferred close for the given SessionFactory.

Parameters:
sessionFactory - Hibernate SessionFactory
See Also:
initDeferredClose(net.sf.hibernate.SessionFactory), closeSessionIfNecessary(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory)

closeSessionIfNecessary

public static void closeSessionIfNecessary(Session session,
                                           SessionFactory sessionFactory)
Close the given Session, created via the given factory, if it isn't bound to the thread.

Parameters:
session - Session to close
sessionFactory - Hibernate SessionFactory that the Session was created with


Copyright (C) 2003-2004 The Spring Framework Project.