Thread:
 web.xml filters don't work 
 mschipperheyn   10 Sep 2005, 19:53 
 Spring's OpenSessionInViewFilter 
 rlubbat   13 Sep 2005, 03:46 
 Re: web.xml filters don't work 
 mgj2   13 Sep 2005, 11:44 
 Re: web.xml filters don't work 
 pksiv   14 Oct 2005, 13:27 
 ThreadLocal Sessions and jsp:include 
 rbellia   25 Oct 2005, 11:41 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: rlubbat (13 Sep 2005, 03:46) Replies: 0, Views: 54195
Subject: Spring's OpenSessionInViewFilter
An alternative solution that you don't need to implement yourself is to
use the Spring Franmework's OpenSessionInViewFilter class. You simply
define a servlet filter mapping for the filter and it takes care of
opening a Session when a request comes in and closing it when the
response is generated.

In fact, it will even bind the Session to the current thread and Spring
provides a static SessionFactoryUtils.getSession() method that
intelligently returns the bound Session or a new one (optionally).

We use this pattern in our Struts application and it works like a charm.
You can reliably get the one Session even if you use jsp:include and you
can pass around your Hibernate POJOs without worrying about lazy
initialization exceptions anywhere.

However, be aware that having a single Session open for an entire
request/response cycle can result in some unexpected behavior that did
not happen before. We encountered this ourselves, but dealt with it by
using the JUnit setUp() and tearDown() methods to mimic the
OpenSessionInViewFilter behavior in our unit tests and solving the
quirks from there.

Hibernate + Spring = bliss
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]