|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.orm.hibernate.SessionFactoryUtils
public abstract class SessionFactoryUtils
Helper 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
(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 in this package requires Hibernate 2.1.
Dedicated Hibernate3 support can be found in a separate package:
org.springframework.orm.hibernate3
.
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(net.sf.hibernate.Criteria criteria,
net.sf.hibernate.SessionFactory sessionFactory)
Apply the current transaction timeout, if any, to the given Hibernate Criteria object. |
static void |
applyTransactionTimeout(net.sf.hibernate.Query query,
net.sf.hibernate.SessionFactory sessionFactory)
Apply the current transaction timeout, if any, to the given Hibernate Query object. |
static void |
closeSession(net.sf.hibernate.Session session)
Perform actual closing of the Hibernate Session, catching and logging any cleanup exceptions thrown. |
static DataAccessException |
convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy. |
static DataSource |
getDataSource(net.sf.hibernate.SessionFactory sessionFactory)
Determine the DataSource of the given SessionFactory. |
static TransactionManager |
getJtaTransactionManager(net.sf.hibernate.SessionFactory sessionFactory,
net.sf.hibernate.Session session)
Try to retrieve the JTA TransactionManager from the given SessionFactory and/or Session. |
static net.sf.hibernate.Session |
getNewSession(net.sf.hibernate.SessionFactory sessionFactory)
Get a new Hibernate Session from the given SessionFactory. |
static net.sf.hibernate.Session |
getNewSession(net.sf.hibernate.SessionFactory sessionFactory,
net.sf.hibernate.Interceptor entityInterceptor)
Get a new Hibernate Session from the given SessionFactory. |
static net.sf.hibernate.Session |
getSession(net.sf.hibernate.SessionFactory sessionFactory,
boolean allowCreate)
Get a Hibernate Session for the given SessionFactory. |
static net.sf.hibernate.Session |
getSession(net.sf.hibernate.SessionFactory sessionFactory,
net.sf.hibernate.Interceptor entityInterceptor,
SQLExceptionTranslator jdbcExceptionTranslator)
Get a Hibernate Session for the given SessionFactory. |
static void |
initDeferredClose(net.sf.hibernate.SessionFactory sessionFactory)
Initialize deferred close for the current thread and the given SessionFactory. |
static boolean |
isDeferredCloseActive(net.sf.hibernate.SessionFactory sessionFactory)
Return if deferred close is active for the current thread and the given SessionFactory. |
static boolean |
isSessionTransactional(net.sf.hibernate.Session session,
net.sf.hibernate.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(net.sf.hibernate.SessionFactory sessionFactory)
Create an appropriate SQLExceptionTranslator for the given SessionFactory. |
static void |
processDeferredClose(net.sf.hibernate.SessionFactory sessionFactory)
Process Sessions that have been registered for deferred close for the given SessionFactory. |
static void |
releaseSession(net.sf.hibernate.Session session,
net.sf.hibernate.SessionFactory sessionFactory)
Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int SESSION_SYNCHRONIZATION_ORDER
DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER
,
Constant Field ValuesConstructor Detail |
---|
public SessionFactoryUtils()
Method Detail |
---|
public static DataSource getDataSource(net.sf.hibernate.SessionFactory sessionFactory)
sessionFactory
- the SessionFactory to check
null
if none foundSessionFactoryImplementor.getConnectionProvider()
,
LocalDataSourceConnectionProvider
public static SQLExceptionTranslator newJdbcExceptionTranslator(net.sf.hibernate.SessionFactory sessionFactory)
sessionFactory
- the SessionFactory to create the translator for
getDataSource(net.sf.hibernate.SessionFactory)
,
SQLErrorCodeSQLExceptionTranslator
,
SQLStateSQLExceptionTranslator
public static TransactionManager getJtaTransactionManager(net.sf.hibernate.SessionFactory sessionFactory, net.sf.hibernate.Session session)
sessionFactory
- Hibernate SessionFactorysession
- Hibernate Session (can also be null
)
TransactionManager
,
SessionFactoryImplementor.getTransactionManager()
,
Session.getSessionFactory()
,
net.sf.hibernate.impl.SessionFactoryImpl
,
net.sf.hibernate.jca.JCASessionFactoryImpl
public static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory, boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException
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.
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.
sessionFactory
- Hibernate SessionFactory to create the session withallowCreate
- if a non-transactional Session should be created when no
transactional Session can be found for the current thread
DataAccessResourceFailureException
- if the Session couldn't be created
IllegalStateException
- if no thread-bound Session found and
"allowCreate" is false
releaseSession(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory)
,
HibernateTemplate
public static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory, net.sf.hibernate.Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException
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.
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.
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.
sessionFactory
- Hibernate SessionFactory to create the session withentityInterceptor
- Hibernate entity interceptor, or null
if nonejdbcExceptionTranslator
- SQLExceptionTranslator to use for flushing the
Session on transaction synchronization (can be null
; only used when actually
registering a transaction synchronization)
DataAccessResourceFailureException
- if the Session couldn't be createdLocalSessionFactoryBean.setEntityInterceptor(net.sf.hibernate.Interceptor)
,
HibernateAccessor.setEntityInterceptor(net.sf.hibernate.Interceptor)
,
HibernateAccessor.setEntityInterceptor(net.sf.hibernate.Interceptor)
,
HibernateTransactionManager
,
JtaTransactionManager
,
TransactionSynchronizationManager
public static net.sf.hibernate.Session getNewSession(net.sf.hibernate.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 with
public static net.sf.hibernate.Session getNewSession(net.sf.hibernate.SessionFactory sessionFactory, net.sf.hibernate.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 none
public static boolean isSessionTransactional(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory)
session
- the Hibernate Session to checksessionFactory
- Hibernate SessionFactory that the Session was created with
(can be null
)
public static void applyTransactionTimeout(net.sf.hibernate.Query query, net.sf.hibernate.SessionFactory sessionFactory)
query
- the Hibernate Query objectsessionFactory
- Hibernate SessionFactory that the Query was created for
(can be null
)Query.setTimeout(int)
public static void applyTransactionTimeout(net.sf.hibernate.Criteria criteria, net.sf.hibernate.SessionFactory sessionFactory)
criteria
- the Hibernate Criteria objectsessionFactory
- Hibernate SessionFactory that the Criteria was created forCriteria.setTimeout(int)
public static DataAccessException convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
org.springframework.dao
hierarchy. Note that it is advisable to
handle JDBCException specifically by using a SQLExceptionTranslator for the
underlying SQLException.
ex
- HibernateException that occured
HibernateAccessor.convertHibernateAccessException(net.sf.hibernate.HibernateException)
,
HibernateAccessor.convertJdbcAccessException(net.sf.hibernate.JDBCException)
,
HibernateTransactionManager.convertHibernateAccessException(net.sf.hibernate.HibernateException)
,
HibernateTransactionManager.convertJdbcAccessException(net.sf.hibernate.JDBCException)
,
JDBCException.getSQLException()
,
SQLExceptionTranslator
public static boolean isDeferredCloseActive(net.sf.hibernate.SessionFactory sessionFactory)
sessionFactory
- the Hibernate SessionFactory to checkpublic static void initDeferredClose(net.sf.hibernate.SessionFactory sessionFactory)
Used by OpenSessionInViewFilter and OpenSessionInViewInterceptor when not configured for a single session.
sessionFactory
- Hibernate SessionFactoryprocessDeferredClose(net.sf.hibernate.SessionFactory)
,
releaseSession(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory)
,
OpenSessionInViewFilter.setSingleSession(boolean)
,
OpenSessionInViewInterceptor.setSingleSession(boolean)
public static void processDeferredClose(net.sf.hibernate.SessionFactory sessionFactory)
sessionFactory
- Hibernate SessionFactoryinitDeferredClose(net.sf.hibernate.SessionFactory)
,
releaseSession(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory)
public static void releaseSession(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory)
session
- the Hibernate Session to close (may be null
)sessionFactory
- Hibernate SessionFactory that the Session was created with
(can be null
)public static void closeSession(net.sf.hibernate.Session session)
session
- the Hibernate Session to close (may be null
)Session.close()
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |