You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Lionel <co...@free.fr> on 2004/05/05 12:31:17 UTC

Struts and Hibernate Session

Hi !

I use struts 1.2 and Hibernate on Websphere 5.
I use the struts plugin to store the SessionFactory on applicaiton context.

Where is the best way to keep the current opened Hibernate Session ?
-in the HttpSession of each user ?
-in a singleton ?
-...

When to close this Session ?
-after each request ?
-when the HttpSession closes ?
-...

Acutally, I have put the getHibernateSession() on a ActionFormBase.
After each SQL action (load, find...) I close the Session.
I don't believe this is a good design and would like to improve it.

Thanks for your advices...

Lionel




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Re[2]: Struts and Hibernate Session

Posted by Lionel <co...@free.fr>.
Carl-Eric Menzel wrote:
> It is possible to reassociate an object from an older session with the
> current session by using the Session.lock() method. See
>
>
http://www.hibernate.org/hib_docs/reference/en/html/manipulatingdata.html#manipulatingdata-update-lock
>

GREAT !!!!!!
This is EXACTLY what I was looking for !!!
thanks a lot !




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re[2]: Struts and Hibernate Session

Posted by Carl-Eric Menzel <cm...@bitforce.com>.
> At 1:57 PM +0200 5/5/04, Lionel wrote:
>>The problem is that when I load an object, put the object into HttpSession,
>>close the hibernate Session.
>>If later I call a getter on lazy loaded children, I get a LazyException,
>>connection closed....
>>I want to keep the children lazy loaded because I scarcely need them.
>>
>>The only solution I found is to open a new Session, reload the object, and
>>then load the children...

> I'm not a Hibernate expert by any means, but I think you would just 
> have to code around it.  I've seen lots of things suggesting that 
> holding on to a Hibernate session for too long causes its own 
> problems, and if you scarcely need the children, you can probably 
> afford the overhead to get them explicitly when you need them.

It is possible to reassociate an object from an older session with the
current session by using the Session.lock() method. See

http://www.hibernate.org/hib_docs/reference/en/html/manipulatingdata.html#manipulatingdata-update-lock

HTH
Carl-Eric
-- 
Antwort: Weil es das Lesen des Textes erschwert.   | Carl-Eric Menzel
Frage  : Warum ist das so schlimm?                 | PGP ID: 808F4A8E
Antwort: Antworten oben zu schreiben.              | Bitte keine HTML-
Frage  : Was ist die schlimmste Unsitte in Emails? | Mails schicken.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and Hibernate Session

Posted by Joe Germuska <Jo...@Germuska.com>.
At 2:10 PM +0200 5/5/04, Lionel wrote:
>Nicolas de Amorrortu wrote:
>>  Have you tried the following? http://www.hibernate.org/43.html.
>
>this is what I was thinking about when I posted my question...but haven't
>tried it yet.
>I don't think it will solve my problem as the object I load is put in
>session and can be accessed many requests later...

That solution offers no help to beans which span multiple HTTP requests.

At 1:57 PM +0200 5/5/04, Lionel wrote:
>The problem is that when I load an object, put the object into HttpSession,
>close the hibernate Session.
>If later I call a getter on lazy loaded children, I get a LazyException,
>connection closed....
>I want to keep the children lazy loaded because I scarcely need them.
>
>The only solution I found is to open a new Session, reload the object, and
>then load the children...

I'm not a Hibernate expert by any means, but I think you would just 
have to code around it.  I've seen lots of things suggesting that 
holding on to a Hibernate session for too long causes its own 
problems, and if you scarcely need the children, you can probably 
afford the overhead to get them explicitly when you need them.

Joe
-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
       "Imagine if every Thursday your shoes exploded if you tied them 
the usual way.  This happens to us all the time with computers, and 
nobody thinks of complaining."
             -- Jef Raskin

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and Hibernate Session

Posted by Lionel <co...@free.fr>.
Nicolas de Amorrortu wrote:
> Have you tried the following? http://www.hibernate.org/43.html.

this is what I was thinking about when I posted my question...but haven't
tried it yet.
I don't think it will solve my problem as the object I load is put in
session and can be accessed many requests later...




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts and Hibernate Session

Posted by Nicolas de Amorrortu <na...@interbanking.com.ar>.
Have you tried the following? http://www.hibernate.org/43.html.

Regards,
Nicolás

-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Lionel
Sent: Miércoles, 05 de Mayo de 2004 08:58 a.m.
To: user@struts.apache.org
Subject: Re: Struts and Hibernate Session


Joe Germuska wrote:

>> Acutally, I have put the getHibernateSession() on a ActionFormBase. 
>> After each SQL action (load, find...) I close the Session. I don't 
>> believe this is a good design and would like to improve it.
>
> This is actually how Hibernate is meant to be used.  The session 
> lifespan is supposed to approximate that of a transaction.

The problem is that when I load an object, put the object into
HttpSession, close the hibernate Session. If later I call a getter on
lazy loaded children, I get a LazyException, connection closed.... I
want to keep the children lazy loaded because I scarcely need them.

The only solution I found is to open a new Session, reload the object,
and then load the children...




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

NOTA DE CONFIDENCIALIDAD
Este mensaje (y sus anexos) es confidencial, esta dirigido exclusivamente a las personas direccionadas en el mail y puede contener informacion (i)de propiedad exclusiva de Interbanking S.A. o (ii) amparada por el secreto profesional. Cualquier opinion en el contenido, es exclusiva de su autor y no representa necesariamente la opinion de Interbanking S.A. El acceso no autorizado, uso, reproduccion, o divulgacion esta prohibido. Interbanking S.A no asumira responsabilidad ni obligacion legal alguna por cualquier informacion incorrecta o alterada contenida en este mensaje. Si usted ha recibido este mensaje por error, le rogamos tenga la amabilidad de destruirlo inmediatamente junto con todas las copias del mismo, notificando al remitente. No debera utilizar, revelar, distribuir, imprimir o copiar este mensaje ni ninguna de sus partes si usted no es el destinatario. Muchas gracias.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and Hibernate Session

Posted by Lionel <co...@free.fr>.
Joe Germuska wrote:

>> Acutally, I have put the getHibernateSession() on a ActionFormBase.
>> After each SQL action (load, find...) I close the Session.
>> I don't believe this is a good design and would like to improve it.
>
> This is actually how Hibernate is meant to be used.  The session
> lifespan is supposed to approximate that of a transaction.

The problem is that when I load an object, put the object into HttpSession,
close the hibernate Session.
If later I call a getter on lazy loaded children, I get a LazyException,
connection closed....
I want to keep the children lazy loaded because I scarcely need them.

The only solution I found is to open a new Session, reload the object, and
then load the children...




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and Hibernate Session

Posted by Joe Germuska <Jo...@Germuska.com>.
>Where is the best way to keep the current opened Hibernate Session ?
>-in the HttpSession of each user ?
>-in a singleton ?
>-...
>
>When to close this Session ?
>-after each request ?
>-when the HttpSession closes ?
>-...
>
>Acutally, I have put the getHibernateSession() on a ActionFormBase.
>After each SQL action (load, find...) I close the Session.
>I don't believe this is a good design and would like to improve it.

This is actually how Hibernate is meant to be used.  The session 
lifespan is supposed to approximate that of a transaction.

Here's one resource.  http://blog.hibernate.org/cgi-bin/blosxom.cgi/2004/01/27

Another (where I learned this originally) is the free sample chapter 
of O'Reilly's "Hibernate Developer's Notebook"  (see 
http://www.oreilly.com/catalog/0596006969/ for download)

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
       "Imagine if every Thursday your shoes exploded if you tied them 
the usual way.  This happens to us all the time with computers, and 
nobody thinks of complaining."
             -- Jef Raskin

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts and Hibernate Session

Posted by Marco Mistroni <mm...@waersystems.com>.
Hi,
	My 2 cents....
I have written an app running on jboss that uses hibernate.
The code that uses hibernate is embedded in a plugin, and a
Session is open and closed after every request

I m also waiting for suggestion on the topic :-)

Regards
	marco



-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Lionel
Sent: 05 May 2004 11:31
To: user@struts.apache.org
Subject: Struts and Hibernate Session

Hi !

I use struts 1.2 and Hibernate on Websphere 5.
I use the struts plugin to store the SessionFactory on applicaiton
context.

Where is the best way to keep the current opened Hibernate Session ?
-in the HttpSession of each user ?
-in a singleton ?
-...

When to close this Session ?
-after each request ?
-when the HttpSession closes ?
-...

Acutally, I have put the getHibernateSession() on a ActionFormBase.
After each SQL action (load, find...) I close the Session.
I don't believe this is a good design and would like to improve it.

Thanks for your advices...

Lionel




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org