The Spring framework provides a servlet filter called
OpenSessionInViewFilter.  All this filter does is open a Hibernate
session and attach it to the thread via a ThreadLocal for every
request.  This gives you access to an open session for the entire
lifespan of the request/response cycle.

The beauty of Spring is that it's so modular that you could actually
implement just this filter and use no other features.  The other
option is to implement your own filter that does the same thing, up to
you.  I've been using it successfully for a long time now in
production.  You just have to be careful to re-attach any objects to
the session before you try and read their data.  HTH...

Cheers,
Mark

On 7/28/05, Pablo Ruggia <[EMAIL PROTECTED]> wrote:
> that thing in your BaseEngine will not work. I forgot to tell you why are
> you getting this exception. When you load an object, then close session, and
> then try to read some property that have not been intialized the first time,
> then you got that exception. It's because hibernate's session is closed or
> disconnected.
> If you are in a deathline, you can put "lazy=false" in all your mapping
> classes and in all your collections mappings. It'will work like a charm. But
> it's not a good decision if you care about performance.
> 
> 
> On 7/28/05, Pablo Ruggia <[EMAIL PROTECTED]> wrote:
> >
> > I've wrote a little article on some solutions at
> > http://pruggia.blogspot.com
> >
> > On 7/28/05, Chris Chiappone < [EMAIL PROTECTED]> wrote:
> > >
> > > I have been developing an application using tapestry 3.0.3 and
> > > hibernate 3.0.2. Took a while to get everything working correctly,
> > > and learning both of the frameworks. I have a decent amount of the
> > > application written and now am running into
> > > LazyInitializationException in certain places. Its strange because I
> > > can't expain why I get the exception and other times I don't.
> > >
> > > I've read that using the Spring Framework would fix this problem, but
> > > wonder if its worth learning another framework and trying to integrate
> > > it into my application. I'm on a deadline and this may just take to
> > > long. I've also extended BaseEngine to do this trying to relieve my
> > > problem:
> > >
> > >
> > > protected void cleanupAfterRequest(IRequestCycle cycle) {
> > > try {
> > >
> > > HibernateUtil.commitTransaction ();
> > >
> > > if (killSession) {
> > > try {
> > > HttpSession session =
> > > cycle.getRequestContext()
> > > .getSession();
> > >
> > > if (session != null) {
> > > log.info("Logging out user.");
> > > session.invalidate();
> > > }
> > > } catch (IllegalStateException ex) {
> > > // Ignore.
> > > }
> > >
> > > }
> > > } catch (NestedRuntimeException nre) {
> > >
> > > } finally {
> > > HibernateUtil.closeSession();
> > > }
> > > }
> > > Yet I still run into this exception:
> > >
> > > Unable to resolve expression ' currentCompany.name' for
> > > [EMAIL PROTECTED]
> > > binding: ExpressionBinding[EditProfile currentCompany.name]
> > > location: context:/WEB-INF/EditProfile.page, line 41, column 65
> > >
> > > ognl.OgnlException
> > > name
> > >
> > > org.hibernate.LazyInitializationException
> > > could not initialize proxy - the owning Session was closed
> > > messages: [Ljava.lang.String;@cdf872
> > > throwableCount: 1
> > > throwables: [Ljava.lang.Throwable;@7227a8
> > > Stack Trace:
> > > org.hibernate.proxy.AbstractLazyInitializer.initialize(
> > > AbstractLazyInitializer.java:53)
> > > org.hibernate.proxy.AbstractLazyInitializer.getImplementation(
> > > AbstractLazyInitializer.java:84)
> > > org.hibernate.proxy.CGLIBLazyInitializer.intercept (
> > > CGLIBLazyInitializer.java:134)
> > > domain.company.Company$$EnhancerByCGLIB$$f029e618.getName(<generated>)
> > > sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > > sun.reflect.NativeMethodAccessorImpl.invoke (
> > > NativeMethodAccessorImpl.java:39)
> > > sun.reflect.DelegatingMethodAccessorImpl.invoke(
> > > DelegatingMethodAccessorImpl.java:25)
> > > java.lang.reflect.Method.invoke(Method.java:585)
> > >
> > >
> > > Is there anything else I should try. Or is the spring framework the
> > > way to go. If so is it easy to integrate into an existing
> > > applicatoin? Thanks.
> > > --
> > > ~chris
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > >
> >
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to