java.lang.Object | |
↳ | org.springframework.orm.hibernate3.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
(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.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | SESSION_SYNCHRONIZATION_ORDER | Order value for TransactionSynchronization objects that clean up Hibernate Sessions. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Apply the current transaction timeout, if any, to the given
Hibernate Query object.
| |||||||||||
Apply the current transaction timeout, if any, to the given
Hibernate Criteria object.
| |||||||||||
Perform actual closing of the Hibernate Session,
catching and logging any cleanup exceptions thrown.
| |||||||||||
Convert the given HibernateException to an appropriate exception
from the
org.springframework.dao hierarchy. | |||||||||||
Get a Hibernate Session for the given SessionFactory.
| |||||||||||
Determine the DataSource of the given SessionFactory.
| |||||||||||
Try to retrieve the JTA TransactionManager from the given SessionFactory
and/or Session.
| |||||||||||
Get a new Hibernate Session from the given SessionFactory.
| |||||||||||
Get a new Hibernate Session from the given SessionFactory.
| |||||||||||
Get a Hibernate Session for the given SessionFactory.
| |||||||||||
Get a Hibernate Session for the given SessionFactory.
| |||||||||||
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.
| |||||||||||
Initialize deferred close for the current thread and the given SessionFactory.
| |||||||||||
Determine whether deferred close is active for the current thread
and the given SessionFactory.
| |||||||||||
Return whether the given Hibernate Session is transactional, that is,
bound to the current thread by Spring's transaction facilities.
| |||||||||||
Create an appropriate SQLExceptionTranslator for the given SessionFactory.
| |||||||||||
Process all Hibernate Sessions that have been registered for deferred close
for the given SessionFactory.
| |||||||||||
Close the given Session, created via the given factory,
if it is not managed externally (i.e.
| |||||||||||
Stringify the given Session for debug logging.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
Returns DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100
to execute Session cleanup before JDBC Connection cleanup, if any.
Apply the current transaction timeout, if any, to the given Hibernate Query object.
query | the Hibernate Query object |
---|---|
sessionFactory | Hibernate SessionFactory that the Query was created for
(may be null ) |
Apply the current transaction timeout, if any, to the given Hibernate Criteria object.
criteria | the Hibernate Criteria object |
---|---|
sessionFactory | Hibernate SessionFactory that the Criteria was created for |
Perform actual closing of the Hibernate Session, catching and logging any cleanup exceptions thrown.
session | the Hibernate Session to close (may be null ) |
---|
Convert the given HibernateException to an appropriate exception
from the org.springframework.dao
hierarchy.
ex | HibernateException that occured |
---|
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
.
Throws the original HibernateException, in contrast to getSession(SessionFactory, Interceptor, SQLExceptionTranslator)
.
sessionFactory | Hibernate SessionFactory to create the session with |
---|---|
allowCreate | whether a non-transactional Session should be created when no transactional Session can be found for the current thread |
if the Session couldn't be created | |
IllegalStateException | if no thread-bound Session found and allowCreate false |
HibernateException |
Determine the DataSource of the given SessionFactory.
sessionFactory | the SessionFactory to check |
---|
null
if none foundLocalDataSourceConnectionProvider
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.
sessionFactory | Hibernate SessionFactory |
---|---|
session | Hibernate Session (can also be null ) |
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.
sessionFactory | Hibernate SessionFactory to create the session with |
---|---|
entityInterceptor | Hibernate entity interceptor, or null if none |
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.
sessionFactory | Hibernate SessionFactory to create the session with |
---|
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 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 with |
---|---|
entityInterceptor | Hibernate entity interceptor, or null if none |
jdbcExceptionTranslator | 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 created |
---|
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 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 with |
---|---|
allowCreate | whether 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 |
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.
sessionFactory | Hibernate SessionFactory to check (may be null ) |
---|
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(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 for |
---|
Determine whether deferred close is active for the current thread and the given SessionFactory.
sessionFactory | the Hibernate SessionFactory to check |
---|
Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.
session | the Hibernate Session to check |
---|---|
sessionFactory | Hibernate SessionFactory that the Session was created with
(may be null ) |
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.
sessionFactory | the SessionFactory to create the translator for |
---|
Process all Hibernate Sessions that have been registered for deferred close for the given SessionFactory.
sessionFactory | the Hibernate SessionFactory to process deferred close for |
---|
Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).
session | the Hibernate Session to close (may be null ) |
---|---|
sessionFactory | Hibernate SessionFactory that the Session was created with
(may be null )
|
Stringify the given Session for debug logging.
Returns output equivalent to 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 stringify |
---|