FileDocCategorySizeDatePackage
Persistence.javaAPI DocExample3486Mon Feb 06 09:17:10 GMT 2006com.jadecove.util

Persistence

public class Persistence extends Object implements Filter
Filter which manages a ThreadLocal hibernate session. Obtain the session by calling Persistance.getSession().

Fields Summary
protected static ThreadLocal
hibernateHolder
Holds the current hibernate session, if one has been created.
protected static SessionFactory
factory
Constructors Summary
Methods Summary
public voiddestroy()

      // Nothing necessary
  
public voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)

      if (hibernateHolder.get() != null)
          throw new IllegalStateException(
              "A session is already associated with this thread!  "
              + "Someone must have called getSession() outside of the context "
              + "of a servlet request.");
          
      try
      {
          chain.doFilter(request, response);
      }
      finally
      {
          Session sess = (Session)hibernateHolder.get();
          if (sess != null)
          {
              hibernateHolder.set(null);
              
              try
              {
                  sess.close();
              }
              catch (HibernateException ex) { throw new ServletException(ex); }
          }
      }
  
public static voiddoInit()
This method should only be called when this class is used directly -- that is, when using this class outside of the servlet container.

throws
HibernateException

		factory = new Configuration().configure().buildSessionFactory();
	
public static SessiongetSession()
ONLY ever call this method from within the context of a servlet request (specifically, one that has been associated with this filter). If you want a Hibernate session at some other time, call getSessionFactory() and open/close the session yourself.

return
an appropriate Session object

      Session sess = (Session)hibernateHolder.get();
      
      if (sess == null)
      {
          sess = factory.openSession();
          hibernateHolder.set(sess);
      }
      
      return sess;
  
public static SessionFactorygetSessionFactory()

return
the hibernate session factory

      return factory;
  
public voidinit(javax.servlet.FilterConfig filterConfig)

  
     
        
      // Initialize hibernate
      try {
				doInit();
      }
      catch (HibernateException ex) { 
      	throw new ServletException(ex); 
      }      
   
public static voidrollback(net.sf.hibernate.Transaction tx)
This is a simple method to reduce the amount of code that needs to be written every time hibernate is used.

      if (tx != null)
      {
          try
          {
              tx.rollback();
          }
          catch (HibernateException ex)
          {
              // Probably don't need to do anything - this is likely being
              // called because of another exception, and we don't want to
              // mask it with yet another exception.
          }
      }