You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Joel McConaughy <jo...@displayware.com> on 2004/08/04 03:20:44 UTC

Where to store a hibernate session in Flow?

I would like to open one hibernate session for each user session and 
close it when the session is invalidated or times out.  I can see how 
save the hibernate session in Flow using cocoon.session.setAttribute() 
and cocoon.session.getAttribute() however there doesn't seem to be a way 
to get notified when the session gets invalidated so I can close the 
hibernate session.   Is there a way to do this?  Thanks.

Joel



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where to store a hibernate session in Flow?

Posted by Brent Johnson <bl...@gmail.com>.
You should use the HttpSessionBindingListener for closing your
hibernate session when the user session expires.  You can use the
HttpSessionListener if you want, but when sessionDestroyed() gets
called the session is already gone.  So it doesnt help much.

Use HttpSessionBindingListener.valueUnbound().  This will get called
just before the session is destroyed so you can read your session. 
I'm doing something similar with hibernate and this seems to work
well.  In flowscript Im doing:
cocoon.session.setAttribute("listener", new
com.li.wedge.client.WedgeSessionListener());

The session listener has to be part of the session itself to get called.

Hope this helps,

- Brent

On Tue, 03 Aug 2004 19:34:08 -0700, Joel McConaughy
<jo...@displayware.com> wrote:
> Good to hear from you, Mark.  Actually, what I'd like to do is keep the
> session factory open during the course of either the session or
> application context.  Then I can just open and close sessions as needed
> -- hopefully providing a performance improvement.  If there really isn't
> any way to get notified when the session or application context get
> invalidated so I can close the factory, then they are pretty useless IMO.
> 
> As an alternative, I could write an HttpSessionListener implementation
> however since this is called by the container (configured in the web.xml
> file), I don't know how to access it from Flow.
> 
> This is one of those things that seems like it should be simple.  Any
> ideas?  Thanks in advance.
> 
> 
> 
> 
> Mark Lundquist wrote:
> 
> >
> > On Aug 3, 2004, at 6:20 PM, Joel McConaughy wrote:
> >
> >> I would like to open one hibernate session for each user session and
> >> close it when the session is invalidated or times out.  I can see how
> >> save the hibernate session in Flow using
> >> cocoon.session.setAttribute() and cocoon.session.getAttribute()
> >> however there doesn't seem to be a way to get notified when the
> >> session gets invalidated so I can close the hibernate session.   Is
> >> there a way to do this?
> >
> >
> > Hi Joel,
> >
> > Just curious, why do you want the Hibernate session to have the same
> > lifetime as the user session?  Typically, in a web application context
> > a Hibernate session should be thought of as corresponding to a single
> > HTTP request/reply.
> >
> > See also this article:
> > http://cocoon.apache.org/2.1/userdocs/forms/index.html
> >
> > Actually, they make mention of the "session-per-user-session
> > anti-pattern" at the end of that article...
> >
> > Cheers,
> > ~ml
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> > For additional commands, e-mail: users-help@cocoon.apache.org
> >
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where to store a hibernate session in Flow?

Posted by Jean-Baptiste Quenot <jb...@anyware-tech.com>.
* Mark Lundquist:

> Why do you need to close the factory?  Just leave it open.
>
> You might also  look at implementing the Thread  Local Session pattern
> (http://hibernate.bluemars.net/42.html).   I  implement  this  pattern
> with a class called HibernateFactory, which wraps the session factory

You're right,  no need  to close  the factory, just  use the  same every
time.  Your solution is good, but Sylvain suggested instead to implement
ThreadSafe in the  HibernateFactory Avalon component, to  ensure that it
is instantiated once.  Is that sufficient?
-- 
Jean-Baptiste Quenot
Anyware Technologies
http://www.anyware-tech.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where to store a hibernate session in Flow?

Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 3, 2004, at 7:34 PM, Joel McConaughy wrote:

> Good to hear from you, Mark.

Likewise :-)

>  Actually, what I'd like to do is keep the session factory open during 
> the course of either the session or application context.  Then I can 
> just open and close sessions as needed -- hopefully providing a 
> performance improvement.  If there really isn't any way to get 
> notified when the session or application context get invalidated so I 
> can close the factory, then they are pretty useless IMO.

Why do you need to close the factory?  Just leave it open.

You might also look at implementing the Thread Local Session pattern 
(http://hibernate.bluemars.net/42.html).  I implement this pattern with 
a class called HibernateFactory, which wraps the session factory like 
this:

     private static SessionFactory sf;

     private static SessionFactory getSessionFactory()
         throws Exception
     {
         if (sf == null) {
             System.out.println ("Initializing HibernateFactory");
             try {
                 net.sf.hibernate.cfg.Configuration cfg;
                 cfg = new Configuration();
				.
				.
				.
                 sf = cfg.buildSessionFactory();
             }
             catch ( Exception e) {
                 System.err.println("Hibernate:" + e.getMessage());
                 e.printStackTrace();
             }
         }
         return sf;
     }

Note that this method is private... nobody outside this class needs the 
SessionFactory.

HTH,
~ml

Re: Where to store a hibernate session in Flow?

Posted by Joel McConaughy <jo...@displayware.com>.
Good to hear from you, Mark.  Actually, what I'd like to do is keep the 
session factory open during the course of either the session or 
application context.  Then I can just open and close sessions as needed 
-- hopefully providing a performance improvement.  If there really isn't 
any way to get notified when the session or application context get 
invalidated so I can close the factory, then they are pretty useless IMO.

As an alternative, I could write an HttpSessionListener implementation 
however since this is called by the container (configured in the web.xml 
file), I don't know how to access it from Flow.

This is one of those things that seems like it should be simple.  Any 
ideas?  Thanks in advance.


Mark Lundquist wrote:

>
> On Aug 3, 2004, at 6:20 PM, Joel McConaughy wrote:
>
>> I would like to open one hibernate session for each user session and 
>> close it when the session is invalidated or times out.  I can see how 
>> save the hibernate session in Flow using 
>> cocoon.session.setAttribute() and cocoon.session.getAttribute() 
>> however there doesn't seem to be a way to get notified when the 
>> session gets invalidated so I can close the hibernate session.   Is 
>> there a way to do this?
>
>
> Hi Joel,
>
> Just curious, why do you want the Hibernate session to have the same 
> lifetime as the user session?  Typically, in a web application context 
> a Hibernate session should be thought of as corresponding to a single 
> HTTP request/reply.
>
> See also this article: 
> http://cocoon.apache.org/2.1/userdocs/forms/index.html
>
> Actually, they make mention of the "session-per-user-session 
> anti-pattern" at the end of that article...
>
> Cheers,
> ~ml
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where to store a hibernate session in Flow?

Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 3, 2004, at 6:20 PM, Joel McConaughy wrote:

> I would like to open one hibernate session for each user session and 
> close it when the session is invalidated or times out.  I can see how 
> save the hibernate session in Flow using cocoon.session.setAttribute() 
> and cocoon.session.getAttribute() however there doesn't seem to be a 
> way to get notified when the session gets invalidated so I can close 
> the hibernate session.   Is there a way to do this?

Hi Joel,

Just curious, why do you want the Hibernate session to have the same 
lifetime as the user session?  Typically, in a web application context 
a Hibernate session should be thought of as corresponding to a single 
HTTP request/reply.

See also this article: 
http://cocoon.apache.org/2.1/userdocs/forms/index.html

Actually, they make mention of the "session-per-user-session 
anti-pattern" at the end of that article...

Cheers,
~ml


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org