@Deprecated
public abstract class SessionFactoryUtils
extends java.lang.Object
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
and HibernateTransactionManager
. Can also be used directly in
application code.
Requires Hibernate 3.6.x, as of Spring 4.0.
getSession(org.hibernate.SessionFactory, boolean)
,
releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,
HibernateTransactionManager
,
JtaTransactionManager
,
TransactionSynchronizationManager
Modifier and Type | Field and Description |
---|---|
static int |
SESSION_SYNCHRONIZATION_ORDER
Deprecated.
Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
|
Constructor and Description |
---|
SessionFactoryUtils()
Deprecated.
|
Modifier and Type | Method and 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 the
org.springframework.dao hierarchy. |
static Session |
doGetSession(SessionFactory sessionFactory,
boolean allowCreate)
Deprecated.
Get a Hibernate Session for the given SessionFactory.
|
static javax.sql.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 java.lang.String |
toString(Session session)
Deprecated.
Stringify the given Session for debug logging.
|
public static final int SESSION_SYNCHRONIZATION_ORDER
DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100
to execute Session cleanup before JDBC Connection cleanup, if any.public static javax.sql.DataSource getDataSource(SessionFactory sessionFactory)
sessionFactory
- the SessionFactory to checknull
if none foundSessionFactoryImplementor.getConnectionProvider()
,
LocalDataSourceConnectionProvider
public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory)
sessionFactory
- the SessionFactory to create the translator forgetDataSource(org.hibernate.SessionFactory)
,
SQLErrorCodeSQLExceptionTranslator
,
SQLStateSQLExceptionTranslator
public static TransactionManager getJtaTransactionManager(SessionFactory sessionFactory, Session session)
sessionFactory
- Hibernate SessionFactorysession
- Hibernate Session (can also be null
)TransactionManager
,
SessionFactoryImplementor.getTransactionManager()
,
Session.getSessionFactory()
,
SessionFactoryImpl
public static Session getSession(SessionFactory sessionFactory, boolean allowCreate) throws DataAccessResourceFailureException, java.lang.IllegalStateException
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 releaseSession
called when done with
the Session. Note that HibernateTemplate allows to write data access code
without caring about such resource handling.
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 threadDataAccessResourceFailureException
- if the Session couldn't be createdjava.lang.IllegalStateException
- if no thread-bound Session found and
"allowCreate" is false
getSession(SessionFactory, Interceptor, SQLExceptionTranslator)
,
releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,
HibernateTemplate
public static Session getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException
HibernateTransactionManager
. 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.
sessionFactory
- Hibernate SessionFactory to create the session withentityInterceptor
- Hibernate entity interceptor, or null
if nonejdbcExceptionTranslator
- SQLExcepionTranslator to use for flushing the
Session on transaction synchronization (may be null
; only used
when actually registering a transaction synchronization)DataAccessResourceFailureException
- if the Session couldn't be createdLocalSessionFactoryBean.setEntityInterceptor(org.hibernate.Interceptor)
,
HibernateAccessor.setEntityInterceptor(org.hibernate.Interceptor)
public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate) throws HibernateException, java.lang.IllegalStateException
HibernateTransactionManager
. Will create a new
Session otherwise, if "allowCreate" is true
.
Throws the original HibernateException, in contrast to getSession(org.hibernate.SessionFactory, boolean)
.
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 threadHibernateException
- if the Session couldn't be createdjava.lang.IllegalStateException
- if no thread-bound Session found and allowCreate falsepublic static Session getNewSession(SessionFactory 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.
sessionFactory
- Hibernate SessionFactory to create the session withpublic static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor)
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.
sessionFactory
- Hibernate SessionFactory to create the session withentityInterceptor
- Hibernate entity interceptor, or null
if nonepublic static java.lang.String toString(Session session)
Object.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).
session
- the Hibernate Session to stringifypublic static boolean hasTransactionalSession(SessionFactory sessionFactory)
sessionFactory
- Hibernate SessionFactory to check (may be null
)public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory)
session
- the Hibernate Session to checksessionFactory
- Hibernate SessionFactory that the Session was created with
(may be null
)public static void applyTransactionTimeout(Query query, SessionFactory sessionFactory)
query
- the Hibernate Query objectsessionFactory
- Hibernate SessionFactory that the Query was created for
(may be null
)Query.setTimeout(int)
public static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory)
criteria
- the Hibernate Criteria objectsessionFactory
- Hibernate SessionFactory that the Criteria was created forCriteria.setTimeout(int)
public static DataAccessException convertHibernateAccessException(HibernateException ex)
org.springframework.dao
hierarchy.ex
- HibernateException that occurredHibernateAccessor.convertHibernateAccessException(org.hibernate.HibernateException)
,
HibernateTransactionManager.convertHibernateAccessException(org.hibernate.HibernateException)
public static boolean isDeferredCloseActive(SessionFactory sessionFactory)
sessionFactory
- the Hibernate SessionFactory to checkpublic static void initDeferredClose(SessionFactory sessionFactory)
processDeferredClose(org.hibernate.SessionFactory)
call at a finishing point (like request completion).
Used by OpenSessionInViewFilter
and OpenSessionInViewInterceptor
when not configured for a single session.
sessionFactory
- the Hibernate SessionFactory to initialize deferred close forprocessDeferredClose(org.hibernate.SessionFactory)
,
releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,
OpenSessionInViewFilter.setSingleSession(boolean)
,
OpenSessionInViewInterceptor.setSingleSession(boolean)
public static void processDeferredClose(SessionFactory sessionFactory)
sessionFactory
- the Hibernate SessionFactory to process deferred close forinitDeferredClose(org.hibernate.SessionFactory)
,
releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
public static void releaseSession(Session session, SessionFactory sessionFactory)
session
- the Hibernate Session to close (may be null
)sessionFactory
- Hibernate SessionFactory that the Session was created with
(may be null
)public static void closeSession(Session session)
session
- the Hibernate Session to close (may be null
)Session.close()