http://www.hibernate.org/43.html
package org.infohazard.pig.servlet;
import java.io.*;
import javax.servlet.*;
import javax.naming.*;
import net.sf.hibernate.*;
/**
* Filter which manages a ThreadLocal hibernate session. Obtain the session
* by calling Persistance.getSession(). Define the JNDI name of the
* hibernate session factory as an init param to the filter in your web.xml.
*
* @author <a href="mailto:jeff@infohazard.org">Jeff Schnitzer</a>
*/
public class Persistance implements Filter
{
/**
* Filter init param which defines the JNDI name for the hibernate factory
*/
public static final String HIBERNATE_FACTORY_JNDI_PARAM = "hibernateFactory";
/**
* Default value if no init param is set.
*/
public static final String HIBERNATE_FACTORY_JNDI_DEFAULT = "java:/HibernateSessionFactory";
/**
* Holds the current hibernate session, if one has been created.
*/
protected static ThreadLocal hibernateHolder = new ThreadLocal();
/**
*/
protected static SessionFactory factory;
/**
*/
public void init(FilterConfig filterConfig) throws ServletException
{
// Initialize hibernate
try
{
new Configuration().configure();
}
catch (HibernateException ex) { throw new ServletException(ex); }
// As good a place as any to initialize the factory
String factoryJndiName = filterConfig.getInitParameter(HIBERNATE_FACTORY_JNDI_PARAM);
if (factoryJndiName == null)
factoryJndiName = HIBERNATE_FACTORY_JNDI_DEFAULT;
try
{
Context ctx = new InitialContext();
factory = (SessionFactory)ctx.lookup(factoryJndiName);
}
catch (NamingException ex) { throw new ServletException(ex); }
}
/**
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
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); }
}
}
}
/**
* 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
*/
public static Session getSession() throws HibernateException
{
Session sess = (Session)hibernateHolder.get();
if (sess == null)
{
sess = factory.openSession();
hibernateHolder.set(sess);
}
return sess;
}
/**
* @return the hibernate session factory
*/
public static SessionFactory getSessionFactory()
{
return factory;
}
/**
* This is a simple method to reduce the amount of code that needs
* to be written every time hibernate is used.
*/
public static void rollback(Transaction tx)
{
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.
}
}
}
/**
*/
public void destroy()
{
// Nothing necessary
}
}
just declare the filter in your web.xml
if you have problem with jndi, modify SessionFactory sf = (SessionFactory) new InitialContext().lookup("SessionFactory")... send me a mail, i'm not at work now.
The methods are static so just call getSession() everywhere you want in a httpcontext.
Once you've done it, ... the most complicated is done in term of managing session, 1 session per httprequest (action) cover 99% of the webapp.