Methods Summary |
---|
public void | destroy()
// Nothing necessary
|
public void | doFilter(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 void | doInit()This method should only be called when this class is used directly -- that is,
when using this class outside of the servlet container.
factory = new Configuration().configure().buildSessionFactory();
|
public static Session | getSession()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.
Session sess = (Session)hibernateHolder.get();
if (sess == null)
{
sess = factory.openSession();
hibernateHolder.set(sess);
}
return sess;
|
public static SessionFactory | getSessionFactory()
return factory;
|
public void | init(javax.servlet.FilterConfig filterConfig)
// Initialize hibernate
try {
doInit();
}
catch (HibernateException ex) {
throw new ServletException(ex);
}
|
public static void | rollback(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.
}
}
|