Class SessionFactoryUtils
- java.lang.Object
- org.springframework.orm.hibernate3.SessionFactoryUtils
@Deprecated public abstract class SessionFactoryUtils extends Object
Deprecated.as of Spring 4.3, in favor of Hibernate 4.x/5.xHelper class featuring methods for Hibernate Session handling, allowing for reuse of Hibernate Session instances within transactions. Also provides support for exception translation.Supports synchronization with both Spring-managed JTA transactions (see
JtaTransactionManager
) and non-Spring JTA transactions (i.e. plain JTA or EJB CMT), transparently providing transaction-scoped Hibernate Sessions. Note that for non-Spring JTA transactions, a JTA TransactionManagerLookup has to be specified in the Hibernate configuration.Used internally by
HibernateTemplate
,HibernateInterceptor
andHibernateTransactionManager
. Can also be used directly in application code.Requires Hibernate 3.6.x, as of Spring 4.0.
Field Summary
Fields Modifier and Type Field Description static int
SESSION_SYNCHRONIZATION_ORDER
Deprecated.Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
Constructor Summary
Constructors Constructor Description SessionFactoryUtils()
Deprecated.
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory)
Deprecated.Apply the current transaction timeout, if any, to the given Hibernate Criteria object.static void
applyTransactionTimeout(Query query, SessionFactory sessionFactory)
Deprecated.Apply the current transaction timeout, if any, to the given Hibernate Query object.static void
closeSession(Session session)
Deprecated.Perform actual closing of the Hibernate Session, catching and logging any cleanup exceptions thrown.static DataAccessException
convertHibernateAccessException(HibernateException ex)
Deprecated.Convert the given HibernateException to an appropriate exception from theorg.springframework.dao
hierarchy.static Session
doGetSession(SessionFactory sessionFactory, boolean allowCreate)
Deprecated.Get a Hibernate Session for the given SessionFactory.static DataSource
getDataSource(SessionFactory sessionFactory)
Deprecated.Determine the DataSource of the given SessionFactory.static TransactionManager
getJtaTransactionManager(SessionFactory sessionFactory, Session session)
Deprecated.Try to retrieve the JTA TransactionManager from the given SessionFactory and/or Session.static Session
getNewSession(SessionFactory sessionFactory)
Deprecated.Get a new Hibernate Session from the given SessionFactory.static Session
getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor)
Deprecated.Get a new Hibernate Session from the given SessionFactory.static Session
getSession(SessionFactory sessionFactory, boolean allowCreate)
Deprecated.Get a Hibernate Session for the given SessionFactory.static Session
getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator)
Deprecated.Get a Hibernate Session for the given SessionFactory.static boolean
hasTransactionalSession(SessionFactory sessionFactory)
Deprecated.Return whether there is a transactional Hibernate Session for the current thread, that is, a Session bound to the current thread by Spring's transaction facilities.static void
initDeferredClose(SessionFactory sessionFactory)
Deprecated.Initialize deferred close for the current thread and the given SessionFactory.static boolean
isDeferredCloseActive(SessionFactory sessionFactory)
Deprecated.Determine whether deferred close is active for the current thread and the given SessionFactory.static boolean
isSessionTransactional(Session session, SessionFactory sessionFactory)
Deprecated.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)
Deprecated.Create an appropriate SQLExceptionTranslator for the given SessionFactory.static void
processDeferredClose(SessionFactory sessionFactory)
Deprecated.Process all Hibernate Sessions that have been registered for deferred close for the given SessionFactory.static void
releaseSession(Session session, SessionFactory sessionFactory)
Deprecated.Close the given Session, created via the given factory, if it is not managed externally (i.e.static String
toString(Session session)
Deprecated.Stringify the given Session for debug logging.
Field Detail
SESSION_SYNCHRONIZATION_ORDER
public static final int SESSION_SYNCHRONIZATION_ORDER
Deprecated.Order value for TransactionSynchronization objects that clean up Hibernate Sessions. ReturnsDataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100
to execute Session cleanup before JDBC Connection cleanup, if any.
Constructor Detail
SessionFactoryUtils
public SessionFactoryUtils()
Deprecated.
Method Detail
getDataSource
public static DataSource getDataSource(SessionFactory sessionFactory)
Deprecated.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)
Deprecated.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(org.hibernate.SessionFactory)
,SQLErrorCodeSQLExceptionTranslator
,SQLStateSQLExceptionTranslator
getJtaTransactionManager
public static TransactionManager getJtaTransactionManager(SessionFactory sessionFactory, Session session)
Deprecated.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.- Parameters:
sessionFactory
- Hibernate SessionFactorysession
- Hibernate Session (can also benull
)- Returns:
- the JTA TransactionManager, if any
- See Also:
TransactionManager
,SessionFactoryImplementor.getTransactionManager()
,Session.getSessionFactory()
,SessionFactoryImpl
getSession
public static Session getSession(SessionFactory sessionFactory, boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException
Deprecated.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 usingHibernateTransactionManager
. Will create a new Session otherwise, if "allowCreate" istrue
.This is the
getSession
method used by typical data access code, in combination withreleaseSession
called when done with the Session. Note that HibernateTemplate allows to write data access code without caring about such resource handling.- Parameters:
sessionFactory
- Hibernate SessionFactory to create the session withallowCreate
- whether a non-transactional Session should be created when no transactional Session can be found for the current thread- Returns:
- the Hibernate Session
- Throws:
DataAccessResourceFailureException
- if the Session couldn't be createdIllegalStateException
- if no thread-bound Session found and "allowCreate" isfalse
- See Also:
getSession(SessionFactory, Interceptor, SQLExceptionTranslator)
,releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,HibernateTemplate
getSession
public static Session getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException
Deprecated.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 usingHibernateTransactionManager
. Will always create a new Session otherwise.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, etc.
- Parameters:
sessionFactory
- Hibernate SessionFactory to create the session withentityInterceptor
- Hibernate entity interceptor, ornull
if nonejdbcExceptionTranslator
- SQLExcepionTranslator to use for flushing the Session on transaction synchronization (may benull
; only used when actually registering a transaction synchronization)- Returns:
- the Hibernate Session
- Throws:
DataAccessResourceFailureException
- if the Session couldn't be created- See Also:
LocalSessionFactoryBean.setEntityInterceptor(org.hibernate.Interceptor)
,HibernateAccessor.setEntityInterceptor(org.hibernate.Interceptor)
doGetSession
public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate) throws HibernateException, IllegalStateException
Deprecated.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 usingHibernateTransactionManager
. Will create a new Session otherwise, if "allowCreate" istrue
.Throws the original HibernateException, in contrast to
getSession(org.hibernate.SessionFactory, boolean)
.- Parameters:
sessionFactory
- Hibernate SessionFactory to create the session withallowCreate
- whether a non-transactional Session should be created when no transactional Session can be found for the current thread- Returns:
- the Hibernate Session
- Throws:
HibernateException
- if the Session couldn't be createdIllegalStateException
- if no thread-bound Session found and allowCreate false
getNewSession
public static Session getNewSession(SessionFactory sessionFactory)
Deprecated.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)
Deprecated.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 withentityInterceptor
- Hibernate entity interceptor, ornull
if none- Returns:
- the new Session
toString
public static String toString(Session session)
Deprecated.Stringify the given Session for debug logging. Returns output equivalent toObject.toString()
: the fully qualified class name + "@" + the identity hash code.The sole reason why this is necessary is because Hibernate3's
Session.toString()
implementation is broken (and won't be fixed): it logs the toString representation of all persistent objects in the Session, which might lead to ConcurrentModificationExceptions if the persistent objects in turn refer to the Session (for example, for lazy loading).- Parameters:
session
- the Hibernate Session to stringify- Returns:
- the String representation of the given Session
hasTransactionalSession
public static boolean hasTransactionalSession(SessionFactory sessionFactory)
Deprecated.Return whether there is a transactional Hibernate Session for the current thread, that is, a Session bound to the current thread by Spring's transaction facilities.- Parameters:
sessionFactory
- Hibernate SessionFactory to check (may benull
)- Returns:
- whether there is a transactional Session for current thread
isSessionTransactional
public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory)
Deprecated.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 checksessionFactory
- Hibernate SessionFactory that the Session was created with (may benull
)- Returns:
- whether the Session is transactional
applyTransactionTimeout
public static void applyTransactionTimeout(Query query, SessionFactory sessionFactory)
Deprecated.Apply the current transaction timeout, if any, to the given Hibernate Query object.- Parameters:
query
- the Hibernate Query objectsessionFactory
- Hibernate SessionFactory that the Query was created for (may benull
)- See Also:
Query.setTimeout(int)
applyTransactionTimeout
public static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory)
Deprecated.Apply the current transaction timeout, if any, to the given Hibernate Criteria object.- Parameters:
criteria
- the Hibernate Criteria objectsessionFactory
- Hibernate SessionFactory that the Criteria was created for- See Also:
Criteria.setTimeout(int)
convertHibernateAccessException
public static DataAccessException convertHibernateAccessException(HibernateException ex)
Deprecated.Convert the given HibernateException to an appropriate exception from theorg.springframework.dao
hierarchy.- Parameters:
ex
- HibernateException that occurred- Returns:
- the corresponding DataAccessException instance
- See Also:
HibernateAccessor.convertHibernateAccessException(org.hibernate.HibernateException)
,HibernateTransactionManager.convertHibernateAccessException(org.hibernate.HibernateException)
isDeferredCloseActive
public static boolean isDeferredCloseActive(SessionFactory sessionFactory)
Deprecated.Determine whether deferred close is active for the current thread and the given SessionFactory.- Parameters:
sessionFactory
- the Hibernate SessionFactory to check- Returns:
- whether deferred close is active
initDeferredClose
public static void initDeferredClose(SessionFactory sessionFactory)
Deprecated.Initialize deferred close for the current thread and the given SessionFactory. Sessions will not be actually closed on close calls then, but rather at aprocessDeferredClose(org.hibernate.SessionFactory)
call at a finishing point (like request completion).Used by
OpenSessionInViewFilter
andOpenSessionInViewInterceptor
when not configured for a single session.- Parameters:
sessionFactory
- the Hibernate SessionFactory to initialize deferred close for- See Also:
processDeferredClose(org.hibernate.SessionFactory)
,releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,OpenSessionInViewFilter.setSingleSession(boolean)
,OpenSessionInViewInterceptor.setSingleSession(boolean)
processDeferredClose
public static void processDeferredClose(SessionFactory sessionFactory)
Deprecated.Process all Hibernate Sessions that have been registered for deferred close for the given SessionFactory.- Parameters:
sessionFactory
- the Hibernate SessionFactory to process deferred close for- See Also:
initDeferredClose(org.hibernate.SessionFactory)
,releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
releaseSession
public static void releaseSession(Session session, SessionFactory sessionFactory)
Deprecated.Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).- Parameters:
session
- the Hibernate Session to close (may benull
)sessionFactory
- Hibernate SessionFactory that the Session was created with (may benull
)
closeSession
public static void closeSession(Session session)
Deprecated.Perform actual closing of the Hibernate Session, catching and logging any cleanup exceptions thrown.- Parameters:
session
- the Hibernate Session to close (may benull
)- See Also:
Session.close()