I'm wondering if there's any problem with the following modifications to Jeff's Filter. The code below uses the HibernateSession object to obtain a session in a Filter. Seems to work pretty well for me. If there are any glaring problems, please let me know. If there aren't any issues with using this *very* simple filter, I'll add it to the wiki.
Thanks, Matt package org.infohazard.pig.servlet; import java.io.*; import javax.servlet.*; import javax.naming.*; import net.sf.hibernate.*; /** * Filter which uses an existing HibernateSession Thread Local class to * obtain a session. Obtain the session in a servlet by calling * Persistance.getSession(). */ public class Persistence implements Filter { /** */ public void init(FilterConfig filterConfig) throws ServletException { // nothing needed here } /** */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { chain.doFilter(request, response); } finally { try { HibernateSession.closeSession(); } 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 { return HibernateSession.currentSession(); } /** */ public void destroy() { // Nothing necessary } } > -----Original Message----- > From: Schnitzer, Jeff [mailto:[EMAIL PROTECTED] > Sent: Wednesday, January 29, 2003 1:49 PM > To: [EMAIL PROTECTED] > Subject: RE: [Hibernate] Obtaining session in a filter > > > > From: Raible, Matt [mailto:[EMAIL PROTECTED] > > > > BTW - your filter complains about setting a static variable > > from a non-static context: this.factory = > > (SessionFactory)ctx.lookup(factoryJndiName); > > Right - that's deliberate. You don't want to create a new > InitialContext and perform a JNDI lookup for the factory > every time you > obtain a new session. It is potentially an expensive > operation. init() > is a great place to do it. > > Alternatively you could cache the factory in a static the > first time you > obtain it. However, that would technically require synchronization to > be thread-safe. > > Jeff > > > ------------------------------------------------------- > This SF.NET email is sponsored by: > SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See! > http://www.vasoftware.com > _______________________________________________ > hibernate-devel mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > ------------------------------------------------------- This SF.NET email is sponsored by: SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See! http://www.vasoftware.com _______________________________________________ hibernate-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/hibernate-devel