/** * HibernateUtil.java * Created on 1 de Junho de 2005, 01:11 * @author Claudio Villar * @author Thiago Morais */ package util; import org.hibernate.*; import org.hibernate.cfg.Configuration; /** * Basic Hibernate helper class, handles SessionFactory, Session and Transaction. *
* Uses a static initializer for the initial SessionFactory creation * and holds Session and Transactions in thread local variables. * * @author christian@hibernate.org */ public class HibernateUtil { // private static Configuration configuration; private static SessionFactory sessionFactory; // private static final ThreadLocal threadSession = new ThreadLocal(); private static final ThreadLocal threadTransaction = new ThreadLocal(); private static final ThreadLocal threadInterceptor = new ThreadLocal(); // Create the initial SessionFactory from the default configuration files static { try { configuration = new Configuration(); sessionFactory = configuration.configure( "/config/hibernate.cfg.xml" ) .buildSessionFactory(); } catch (Throwable exception) { // We have to catch Throwable, otherwise we will miss // NoClassDefFoundError and other subclasses of Error throw new ExceptionInInitializerError( exception ); } } /** * Returns the SessionFactory used for this static class. * * @return SessionFactory */ public static SessionFactory getSessionFactory() { return( sessionFactory ); } /** * Returns the original Hibernate configuration. * * @return Configuration */ public static Configuration getConfiguration() { return( configuration ); } /** * Retrieves the current Session local to the thread. *
* If no Session is open, opens a new Session for the running thread. * * @return Session */ public static Session getSession() { Session session = (Session)(threadSession.get()); /* */ try { if( session == null ) { if( getInterceptor() != null ) session = getSessionFactory().openSession( getInterceptor() ); else session = getSessionFactory().openSession(); threadSession.set( session ); } }catch(HibernateException hibernateException) { throw hibernateException; } return( session ); } /** * Closes the Session local to the thread. */ public static void closeSession() { try { Session session = (Session)(threadSession.get()); threadSession.set( null ); if( session != null && session.isOpen() ) session.close(); }catch( HibernateException hibernateException ) { throw hibernateException; } } /** * Start a new database transaction. */ public static void beginTransaction() { Transaction transaction = (Transaction)(threadTransaction.get()); try { if( transaction == null ) { transaction = getSession().beginTransaction(); threadTransaction.set( transaction ); } }catch( HibernateException hibernateException ) { throw hibernateException; } } /** * Commit the database transaction. */ public static void commitTransaction() { Transaction transaction = (Transaction)(threadTransaction.get()); try { if( transaction != null && !transaction.wasCommitted() && !transaction.wasRolledBack() ) transaction.commit(); threadTransaction.set( null ); }catch(HibernateException hibernateException) { rollbackTransaction(); throw hibernateException; } } /** * Commit the database transaction. */ public static void rollbackTransaction() { Transaction transaction = (Transaction)(threadTransaction.get()); try { threadTransaction.set( null ); if( transaction != null && !transaction.wasCommitted() && !transaction.wasRolledBack() ) transaction.rollback(); }catch(HibernateException hibernateException) { throw hibernateException; }finally { closeSession(); } } public static void initiateSessionTransaction() { Transaction transaction = (Transaction)(threadTransaction.get()); try { if( transaction == null ) { transaction = getSession().beginTransaction(); threadTransaction.set( transaction ); } }catch( HibernateException hibernateException ) { throw hibernateException; } } public static void terminateSessionTransaction() { Transaction transaction = (Transaction)(threadTransaction.get()); try { if( (transaction != null) && (!transaction.wasCommitted()) && (!transaction.wasRolledBack()) ) transaction.commit(); threadTransaction.set( null ); }catch(HibernateException hibernateException) { rollbackTransaction(); throw hibernateException; }finally { closeSession(); } } /** * Reconnects a Hibernate Session to the current Thread. * * @param session The Hibernate Session to be reconnected. */ public static void reconnect(Session session) { try { session.reconnect(); threadSession.set( session ); }catch(HibernateException hibernateException) { throw hibernateException; } } /** * Disconnect and return Session from current Thread. * * @return Session the disconnected Session */ public static Session disconnectSession() { Session session = getSession(); try { threadSession.set( null ); if( session.isConnected() && session.isOpen() ) session.disconnect(); }catch(HibernateException hibernateException) { throw hibernateException; } return( session ); } /** * Register a Hibernate interceptor with the current thread. ** 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()/getSession(). */ public static void registerInterceptor(Interceptor interceptor) { threadInterceptor.set( interceptor ); } private static Interceptor getInterceptor() { Interceptor interceptor = (Interceptor)(threadInterceptor.get()); return( interceptor ); } }