edu.sdsc.inca.depot.persistent
Class HibernateUtil

java.lang.Object
  extended by edu.sdsc.inca.depot.persistent.HibernateUtil

public class HibernateUtil
extends java.lang.Object

Basic Hibernate helper class, handles SessionFactory, Session and Transaction.

Uses a static initializer to read startup options and initialize Configuration and SessionFactory.

This class tries to figure out if either ThreadLocal handling of the Session and Transaction should be used, for resource local transactions or BMT, or if CMT with automatic Session handling is enabled.

To keep your DAOs free from any of this, just call HibernateUtil.getCurrentSession() in the constructor of each DAO., The recommended way to set resource local or BMT transaction boundaries is an interceptor, or a request filter.

This class also tries to figure out if JNDI binding of the SessionFactory is used, otherwise it falls back to a global static variable (Singleton).

If you want to assign a global interceptor, set its fully qualified class name with the system (or hibernate.properties/hibernate.cfg.xml) property hibernate.util.interceptor_class. It will be loaded and instantiated on static initialization of HibernateUtil; it has to have a no-argument constructor. You can call getInterceptor() if you need to provide settings before using the interceptor.

Note: This class supports annotations by default, hence needs JDK 5.0 and the Hibernate Annotations library on the classpath. Change the single commented line in the source to make it compile and run on older JDKs with XML mapping files only.

Author:
christian@hibernate.org

Constructor Summary
HibernateUtil()
           
 
Method Summary
static void beginTransaction()
          Start a new database transaction.
static void closeSession()
          Closes the Session local to the thread.
static void commitTransaction()
          Commit the database transaction.
static org.hibernate.Session disconnectSession()
          Disconnect and return Session from current Thread.
static org.hibernate.cfg.Configuration getConfiguration()
          Returns the original Hibernate configuration.
static org.hibernate.Session getCurrentSession()
          Retrieves the current Session local to the thread.
static org.hibernate.Interceptor getInterceptor()
           
static org.hibernate.SessionFactory getSessionFactory()
          Returns the global SessionFactory.
static void rebuildSessionFactory()
          Rebuild the SessionFactory with the static Configuration.
static void rebuildSessionFactory(org.hibernate.cfg.Configuration cfg)
          Rebuild the SessionFactory with the given Hibernate Configuration.
static void reconnect(org.hibernate.Session session)
          Reconnects a Hibernate Session to the current Thread.
static void registerInterceptorAndRebuild(org.hibernate.Interceptor interceptor)
          Register a Hibernate interceptor with the current SessionFactory.
static void rollbackTransaction()
          Rollback the database transaction.
static void shutdown()
          Closes the current SessionFactory and releases all resources.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateUtil

public HibernateUtil()
Method Detail

getConfiguration

public static org.hibernate.cfg.Configuration getConfiguration()
Returns the original Hibernate configuration.

Returns:
Configuration

getSessionFactory

public static org.hibernate.SessionFactory getSessionFactory()
Returns the global SessionFactory.

Returns:
SessionFactory

shutdown

public static void shutdown()
Closes the current SessionFactory and releases all resources.

The only other method that can be called on HibernateUtil after this one is rebuildSessionFactory(Configuration).


rebuildSessionFactory

public static void rebuildSessionFactory()
Rebuild the SessionFactory with the static Configuration.

This method also closes the old SessionFactory before, if still open. Note that this method should only be used with static SessionFactory management, not with JNDI or any other external registry.


rebuildSessionFactory

public static void rebuildSessionFactory(org.hibernate.cfg.Configuration cfg)
Rebuild the SessionFactory with the given Hibernate Configuration.

HibernateUtil does not configure() the given Configuration object, it directly calls buildSessionFactory(). This method also closes the old SessionFactory before, if still open.

Parameters:
cfg -

getCurrentSession

public static org.hibernate.Session getCurrentSession()
Retrieves the current Session local to the thread.

If no Session is open, opens a new Session for the running thread. If CMT is used, returns the Session bound to the current JTA container transaction. Most other operations on this class will then be no-ops or not supported, the container handles Session and Transaction boundaries, ThreadLocals are not used.

Returns:
Session

closeSession

public static void closeSession()
Closes the Session local to the thread.

Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions.


beginTransaction

public static void beginTransaction()
Start a new database transaction.

Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions. In both cases, it will either start a new transaction or join the existing ThreadLocal or JTA transaction.


commitTransaction

public static void commitTransaction()
Commit the database transaction.

Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions. It will commit the ThreadLocal or BMT/JTA transaction.


rollbackTransaction

public static void rollbackTransaction()
Rollback the database transaction.

Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions. It will rollback the resource local or BMT/JTA transaction.


reconnect

public static void reconnect(org.hibernate.Session session)
Reconnects a Hibernate Session to the current Thread.

Unsupported in a CMT environment.

Parameters:
session - The Hibernate Session to be reconnected.

disconnectSession

public static org.hibernate.Session disconnectSession()
Disconnect and return Session from current Thread.

Returns:
Session the disconnected Session

registerInterceptorAndRebuild

public static void registerInterceptorAndRebuild(org.hibernate.Interceptor interceptor)
Register a Hibernate interceptor with the current SessionFactory.

Every Session opened is opened with this interceptor after registration. Has no effect if the current Session of the thread is already open, effective on next close()/getCurrentSession().

Attention: This method effectively restarts Hibernate. If you need an interceptor active on static startup of HibernateUtil, set the hibernateutil.interceptor system property to its fully qualified class name.


getInterceptor

public static org.hibernate.Interceptor getInterceptor()