com.interface21.orm.hibernate
Class SessionFactoryUtils

java.lang.Object
  |
  +--com.interface21.orm.hibernate.SessionFactoryUtils

public abstract class SessionFactoryUtils
extends java.lang.Object

Helper class featuring methods for Hibernate session handling, allowing for reuse of Hibernate Session instances within transactions. Supports synchronization with JTA transactions via JtaTransactionManager, to allow for proper transactional handling of the JVM-level cache. Used by HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager.

Note: This class, like all of Spring's Hibernate support, requires Hibernate 2.0 (initially developed with RC1).

Since:
02.05.2003
Author:
Juergen Hoeller
See Also:
HibernateTemplate, HibernateInterceptor, HibernateTransactionManager, JtaTransactionManager

Constructor Summary
SessionFactoryUtils()
           
 
Method Summary
static void closeSessionIfNecessary(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory)
          Close the given Session, created via the given factory, if it isn't bound to the thread.
static DataAccessException convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the com.interface21.dao hierarchy.
static net.sf.hibernate.SessionFactory createSessionFactory(java.lang.String configLocation)
          Create a Hibernate SessionFactory with the given config file.
static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory, boolean allowCreate)
          Get a Hibernate Session for the given factory.
static ThreadObjectManager getThreadObjectManager()
          Return the thread object manager for Hibernate sessions, keeping a SessionFactory/SessionHolder map per thread for Hibernate transactions.
static boolean isSessionBoundToThread(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory)
          Return if the given Session is bound to the current thread, for the given SessionFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionFactoryUtils

public SessionFactoryUtils()
Method Detail

getThreadObjectManager

public static ThreadObjectManager getThreadObjectManager()
Return the thread object manager for Hibernate sessions, keeping a SessionFactory/SessionHolder map per thread for Hibernate transactions.
Returns:
the thread object manager
See Also:
getSession(net.sf.hibernate.SessionFactory, boolean), HibernateTransactionManager

isSessionBoundToThread

public static boolean isSessionBoundToThread(net.sf.hibernate.Session session,
                                             net.sf.hibernate.SessionFactory sessionFactory)
Return if the given Session is bound to the current thread, for the given SessionFactory.
Parameters:
session - Session that should be checked
sessionFactory - SessionFactory that the Session was created with
Returns:
if the Session is bound for the SessionFactory

createSessionFactory

public static net.sf.hibernate.SessionFactory createSessionFactory(java.lang.String configLocation)
                                                            throws DataAccessResourceFailureException
Create a Hibernate SessionFactory with the given config file.
Parameters:
configLocation - location of the config file (can be a URL or a classpath resource), or null if default
Returns:
the new SessionFactory instance
Throws:
DataAccessResourceFailureException - if the SessionFactory could not be created

getSession

public static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory,
                                                  boolean allowCreate)
                                           throws DataAccessResourceFailureException,
                                                  java.lang.IllegalStateException
Get a Hibernate Session for the given factory. Is aware of a respective Session bound to the current thread, for example when using HibernateTransactionManager. Will create a new Session else, if allowCreate is true.
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
java.lang.IllegalStateException - if no thread-bound Session found and allowCreate false

convertHibernateAccessException

public static DataAccessException convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
Convert the given HibernateException to an appropriate exception from the com.interface21.dao hierarchy.
Parameters:
ex - HibernateException that occured
Returns:
the corresponding DataAccessException instance

closeSessionIfNecessary

public static void closeSessionIfNecessary(net.sf.hibernate.Session session,
                                           net.sf.hibernate.SessionFactory sessionFactory)
                                    throws CleanupFailureDataAccessException
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
Throws:
DataAccessResourceFailureException - if the Session couldn't be closed


Rod Johnson and Spring contributors 2001-2003.