Jeff, Thanks for adding your filter to the wiki. However, I'm looking for an even simpler solution. I'd like to use Thread Local Session (http://hibernate.bluemars.net/42.html) in combination with a Filter, so I can still obtain sessions in my JUnit tests, and I'm not duplicating code in the web tier. BTW - your filter complains about setting a static variable from a non-static context: this.factory = (SessionFactory)ctx.lookup(factoryJndiName);
Here's the doFilter method I've come up with - and it seems to work just fine. The only problem being that it's opening a session for each request. A better solution would be to declaratively specify which servlets need a connection - or use something like your getSession() method. Thanks, Matt public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Session ses = null; boolean sessionCreated = false; try { // Get the http session id from the request, then we will // try to get the Hiberate Session from the request. If // it doesn't exist, then we will create it, otherwise // we will use the one that already exists. ses = (Session) request.getAttribute(Constants.SESSION_KEY); if (ses == null) { ses = HibernateSession.currentSession(); request.setAttribute(Constants.SESSION_KEY, ses); sessionCreated = true; } } catch (Exception exc) { log.error("Error opening Hibernate session.", exc); exc.printStackTrace(); } try { chain.doFilter(request, response); } finally { try { if (sessionCreated) { HibernateSession.closeSession(); } } catch (Exception exc) { log.error("Error closing hibernate session.", exc); exc.printStackTrace(); } } } > -----Original Message----- > From: Schnitzer, Jeff [mailto:[EMAIL PROTECTED] > Sent: Tuesday, January 28, 2003 6:55 PM > To: [EMAIL PROTECTED] > Subject: RE: [Hibernate] Obtaining session in a filter > > > I just added my filter to the Wiki; I recommend using that :-) > > http://hibernate.bluemars.net/43.html > > Jeff Schnitzer > [EMAIL PROTECTED] > > > -----Original Message----- > > From: Raible, Matt [mailto:[EMAIL PROTECTED] > > Sent: Tuesday, January 28, 2003 4:06 PM > > To: '[EMAIL PROTECTED]' > > Subject: [Hibernate] Obtaining session in a filter > > > > I'm trying to migrate my app to use a Session at the UI > level, and to > pass > > this session down to my services and persistence layers. In my > Filter, > > I'm > > basically calling a HibernateSession class which looks like the > > ThreadLocal > > pattern from the design patterns on the wiki. Below is my doFilter > > method. > > > > The basic problem I'm experiencing can be seen from the > following log > > snip: > > > > DEBUG [Ajp13Processor[11009][4]] > HibernateFilter.doFilter(86) | Closed > > hibernate session. > > DEBUG [Ajp13Processor[11009][3]] > HibernateFilter.doFilter(62) | Opened > > hibernate session. > > DEBUG [Ajp13Processor[11009][3]] > HibernateFilter.doFilter(62) | Opened > > hibernate session. > > DEBUG [Ajp13Processor[11009][3]] CMCFAction.save(117) | Entering > 'save' > > method > > com.comcast.cable.dmc.itd.cct.persistence.DAOException: > > cirrus.hibernate.HibernateException: Session > > is currently disconnected > > > > Is the code below the best way to implement the UI Filter > pattern? It > was > > fairly easy for me to refactor, but I think I'm probably doing > something > > wrong. Part of the refactoring process was to remove all > the finally > > blocks > > from my methods where I used to be calling > > HibernateSession.closeSession(); > > Should I be calling HibernateSession.closeSession() in the finally > block > > of > > this method. > > > > Thanks for any advice, > > > > Matt > > > > public void doFilter(ServletRequest request, ServletResponse > response, > > FilterChain chain) > > throws IOException, ServletException { > > Session ses = null; > > boolean sessionCreated = false; > > > > try { > > // Get the http session id from the request, > then we will > > // try to get the Hiberate Session from the > request. If > > // it doesn't exist, then we will create it, otherwise > > // we will use the one that already exists. > > ses = (Session) > request.getAttribute(Constants.SESSION_KEY); > > > > if (ses == null) { > > ses = HibernateSession.currentSession(); > > > > if (log.isDebugEnabled()) { > > log.debug("Opened hibernate session."); > > } > > > > request.setAttribute(Constants.SESSION_KEY, ses); > > sessionCreated = true; > > } > > } catch (Exception exc) { > > log.error("Error opening Hibernate session.", exc); > > exc.printStackTrace(); > > } > > > > try { > > chain.doFilter(request, response); > > } finally { > > try { > > if (sessionCreated) { > > if (ses != null) { > > // Only try to close the connection if it is > open, > > // since it might have been closed somewhere > else > > // by mistake. > > if (ses.isOpen()) { > > ses.close(); > > > > if (log.isDebugEnabled()) { > > log.debug("Closed hibernate > session."); > > } > > } > > } > > } > > } catch (Exception exc) { > > log.error("Error closing hibernate session.", exc); > > exc.printStackTrace(); > > } > > } > > } > > > > > > > > ------------------------------------------------------- > > 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 > ------------------------------------------------------- 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