You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Riyad Kalla <rs...@email.arizona.edu> on 2004/06/08 01:13:42 UTC

OT: Hibernate Session to User Session

I was reading in the Hibernate forums that when you have a WebApp > DAO
> Hibernate > DB design, and you open/close each Hibernate session at
every single DAO method call (what I do now) its actually quite
expensive, and is suggested that you attempt to maintain a Hibernate
Session in conjunction with a user Session to increase performance.

I had a question for anyone that has done this (without using
SpringFramework). My gut-reaction to this was to add a
HttpSessionListener to my webapp that created and stored a Hibernate
session in the user's session, and then close it when the Session
expired... will this not work? Anyone else have a good solution for
this?

TIA,
Riyad


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


Re: OT: Hibernate Session to User Session

Posted by Axel Stahlhut <as...@neusta.de>.
Riyad Kalla wrote:

>I was reading in the Hibernate forums that when you have a WebApp > DAO
>  
>
>>Hibernate > DB design, and you open/close each Hibernate session at
>>    
>>
>every single DAO method call (what I do now) its actually quite
>expensive, and is suggested that you attempt to maintain a Hibernate
>Session in conjunction with a user Session to increase performance.
>
>I had a question for anyone that has done this (without using
>SpringFramework). My gut-reaction to this was to add a
>HttpSessionListener to my webapp that created and stored a Hibernate
>session in the user's session, and then close it when the Session
>expired... will this not work? Anyone else have a good solution for
>this?
>
>TIA,
>Riyad
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>  
>
Hi.

Thisis not a Hibernate forum... Anyway: If you read the docs, you will 
find good practice not to hold a long during hibernate session. You 
would get problems with synchronisation and consistent hibernate session 
states, furthermore this would result in too many open sessions. (Good 
point for denial of service-attack...)
I prefer using hibernate sessions hold in a ThreadLocal, which is really 
nice because you do not have to get HibernmateSession through all of 
your method signatures. The Session is opened at beginning of the 
Request and closed at the end. I close it in a ServletFilter, so i can 
do within Srtruts whatever i want (declarative Exception handling...) 
and i can be sure it is always closed. This way i also can track the max 
parallel open Hibernate Sessions, which tells a lot over server load.

Regards Axel

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


Re: OT: Hibernate Session to User Session

Posted by Riyad Kalla <rs...@email.arizona.edu>.
Carl,
Thank you for the corrects, yes after you point it out it seems I did 
read something very incorrectly. I appreciate you suggestion at the 
bottom of the message, I will look into this.

Best,
Riyad

Carl-Eric Menzel wrote:

>>I was reading in the Hibernate forums that when you have a WebApp > DAO
>>Hibernate > DB design, and you open/close each Hibernate session at
>>every single DAO method call (what I do now) its actually quite
>>expensive,
>>    
>>
>
>This is fairly expensive, but not that much. The *SessionFactory* is
>expensive, a single Session isn't that bad.
>
>  
>
>>and is suggested that you attempt to maintain a Hibernate
>>Session in conjunction with a user Session to increase performance.
>>    
>>
>
>You have definitely misread something. This is not officially
>recommended anywhere.
>
>  
>
>>I had a question for anyone that has done this (without using
>>SpringFramework). My gut-reaction to this was to add a
>>HttpSessionListener to my webapp that created and stored a Hibernate
>>session in the user's session, and then close it when the Session
>>expired... will this not work? Anyone else have a good solution for
>>this?
>>    
>>
>
>No, this is not going to work. A Session is similar in its scope to a
>transaction - it is what the Hibernate developers call an "Application
>Transaction", i.e. a unit of work from the application's point of
>work, that may span several database transactions, but should NOT be
>kept open during user think-time, i.e. across requests. The biggest
>problem with this is that you would keep a db connection open with
>this as well. If you use the disconnect()/reconnect() methods, you
>wouldn't have the open connection, but since the Session is also a
>local cache you would very quickly see stale data.
>
>The best way to do it, IMO, is to use for example a request filter and
>generate a fresh Session when the request begins, and close it at the
>end of the request. That way you always have fresh data and don't
>unnecessarily keep open connections.
>
>HTH
>Carl-Eric
>
>
>---------------------------------------------------------------------
>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


Re: OT: Hibernate Session to User Session

Posted by Carl-Eric Menzel <cm...@bitforce.com>.
> I was reading in the Hibernate forums that when you have a WebApp > DAO
> Hibernate > DB design, and you open/close each Hibernate session at
> every single DAO method call (what I do now) its actually quite
> expensive,

This is fairly expensive, but not that much. The *SessionFactory* is
expensive, a single Session isn't that bad.

> and is suggested that you attempt to maintain a Hibernate
> Session in conjunction with a user Session to increase performance.

You have definitely misread something. This is not officially
recommended anywhere.

> I had a question for anyone that has done this (without using
> SpringFramework). My gut-reaction to this was to add a
> HttpSessionListener to my webapp that created and stored a Hibernate
> session in the user's session, and then close it when the Session
> expired... will this not work? Anyone else have a good solution for
> this?

No, this is not going to work. A Session is similar in its scope to a
transaction - it is what the Hibernate developers call an "Application
Transaction", i.e. a unit of work from the application's point of
work, that may span several database transactions, but should NOT be
kept open during user think-time, i.e. across requests. The biggest
problem with this is that you would keep a db connection open with
this as well. If you use the disconnect()/reconnect() methods, you
wouldn't have the open connection, but since the Session is also a
local cache you would very quickly see stale data.

The best way to do it, IMO, is to use for example a request filter and
generate a fresh Session when the request begins, and close it at the
end of the request. That way you always have fresh data and don't
unnecessarily keep open connections.

HTH
Carl-Eric


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


Re: Hibernate Session to User Session

Posted by Riyad Kalla <rs...@email.arizona.edu>.
Brian,
I agree that sounds very expensive, I don't know how Hibernate works yet 
so I didn't know what a Hibernate Session really consists of to know if 
that was as resource intensive as it sounded.

If this is the case, then I suppose using the HibernateUtil that I am 
using now from the Hibernate docs is probably the way to go (does the 
ThreadLocal business....)

Brian Alexander Lee wrote:

>Wouldn't that be pretty resource intensive since it would basically mean
>that you have a database connection for every user session in your app.
>Typically that is pretty bad and getting a new connection from a connection
>pool is usually pretty quick.
>
>I'm unfamiliar with how hibernate manges sessions and connections so let me
>know if I'm wrong.
>
>I'm looking at using Hibernate in my struts actions, but also in my ejbs so
>of course I wouldn't be able to tie hibernate sessions with http sessions.
>
>Thanks,
>BAL
>----- Original Message ----- 
>From: "Riyad Kalla" <rs...@email.arizona.edu>
>To: "Struts Users Mailing List" <us...@struts.apache.org>
>Sent: Monday, June 07, 2004 7:13 PM
>Subject: OT: Hibernate Session to User Session
>
>
>  
>
>>I was reading in the Hibernate forums that when you have a WebApp > DAO
>>    
>>
>>>Hibernate > DB design, and you open/close each Hibernate session at
>>>      
>>>
>>every single DAO method call (what I do now) its actually quite
>>expensive, and is suggested that you attempt to maintain a Hibernate
>>Session in conjunction with a user Session to increase performance.
>>
>>I had a question for anyone that has done this (without using
>>SpringFramework). My gut-reaction to this was to add a
>>HttpSessionListener to my webapp that created and stored a Hibernate
>>session in the user's session, and then close it when the Session
>>expired... will this not work? Anyone else have a good solution for
>>this?
>>
>>TIA,
>>Riyad
>>
>>
>>---------------------------------------------------------------------
>>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
>
>  
>

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


Re: Hibernate Session to User Session

Posted by Brian Alexander Lee <br...@hotmail.com>.
Wouldn't that be pretty resource intensive since it would basically mean
that you have a database connection for every user session in your app.
Typically that is pretty bad and getting a new connection from a connection
pool is usually pretty quick.

I'm unfamiliar with how hibernate manges sessions and connections so let me
know if I'm wrong.

I'm looking at using Hibernate in my struts actions, but also in my ejbs so
of course I wouldn't be able to tie hibernate sessions with http sessions.

Thanks,
BAL
----- Original Message ----- 
From: "Riyad Kalla" <rs...@email.arizona.edu>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Monday, June 07, 2004 7:13 PM
Subject: OT: Hibernate Session to User Session


> I was reading in the Hibernate forums that when you have a WebApp > DAO
> > Hibernate > DB design, and you open/close each Hibernate session at
> every single DAO method call (what I do now) its actually quite
> expensive, and is suggested that you attempt to maintain a Hibernate
> Session in conjunction with a user Session to increase performance.
>
> I had a question for anyone that has done this (without using
> SpringFramework). My gut-reaction to this was to add a
> HttpSessionListener to my webapp that created and stored a Hibernate
> session in the user's session, and then close it when the Session
> expired... will this not work? Anyone else have a good solution for
> this?
>
> TIA,
> Riyad
>
>
> ---------------------------------------------------------------------
> 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


RE: Hibernate Session to User Session

Posted by David Friedman <hu...@ix.netcom.com>.
It's not very process intensive if you are using 
a connection pool where it just hands you an 
already-open connection.  Hibernate can use a 
few different connection pools including JCS,
C3P0, OSCache, etc.

Regards,
David

-----Original Message-----
From: Riyad Kalla [mailto:rsk@email.arizona.edu]
Sent: Monday, June 07, 2004 7:14 PM
To: Struts Users Mailing List
Subject: OT: Hibernate Session to User Session


I was reading in the Hibernate forums that when you have a WebApp > DAO
> Hibernate > DB design, and you open/close each Hibernate session at
every single DAO method call (what I do now) its actually quite
expensive, and is suggested that you attempt to maintain a Hibernate
Session in conjunction with a user Session to increase performance.

I had a question for anyone that has done this (without using
SpringFramework). My gut-reaction to this was to add a
HttpSessionListener to my webapp that created and stored a Hibernate
session in the user's session, and then close it when the Session
expired... will this not work? Anyone else have a good solution for
this?

TIA,
Riyad


---------------------------------------------------------------------
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


Re[2]: Hibernate Session to User Session

Posted by Carl-Eric Menzel <cm...@bitforce.com>.
Riyad Kalla wrote:

> I believe that is what I am using now (it seems to be quite popular) but
> I don't quite understand the reasoning behind the ThreadLocal approach
> (I've actually never used 'ThreadLocal')... can you shed some light on
> this? What the mapping between Hibernate Sessions and Users (or Threads)
> becomes?

The preferred mapping is still one Hibernate Session per HTTP Request.
You would also still need a Filter that instantiates the Session when
the request comes in and closes it when it is finished. But instead of
putting it into the request as an attribute, you can put it into a
ThreadLocal. That way, all methods that need the Session can simply
use the ThreadLocal to get it and you don't have to pass around
request attributes.

Carl-Eric


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


RE: Hibernate Session to User Session

Posted by Marco Mistroni <mm...@waersystems.com>.
Hi,
	I m no expert in hibernate :-( , I got suggestions
>From list and hibernate docs...
If u check hibernate reference, it mentions somewhere why
They suggest using ThreadLocal.

If I remember correctly they say that proper use is to use
One session (hibernate session) per request.. then I checked
The Struts plugin for hibernate and hibernate samples..

Sorry for not being of any much help :-(

Regards
	marco

-----Original Message-----
From: Riyad Kalla [mailto:rsk@email.arizona.edu] 
Sent: 08 June 2004 14:54
To: Struts Users Mailing List
Subject: Re: Hibernate Session to User Session

Marco,
I believe that is what I am using now (it seems to be quite popular) but

I don't quite understand the reasoning behind the ThreadLocal approach 
(I've actually never used 'ThreadLocal')... can you shed some light on 
this? What the mapping between Hibernate Sessions and Users (or Threads)

becomes?

Marco Mistroni wrote:

>Hi,
>	On hibernate website, there's an example that associates
>A session to a threadlocal. You can safely use that pattern (it's one
>Of hibernate patterns)...
>You could have a DAOFactory (plugin) which instantiates the
>SessionFactory,
>And each DAO will get its own session associated with its ThreadLocal..
>
>Have a look at hibernate website for patterns on how to use Session
>
>Regards,	marco
>
>-----Original Message-----
>From: Riyad Kalla [mailto:rsk@email.arizona.edu] 
>Sent: 08 June 2004 00:14
>To: Struts Users Mailing List
>Subject: OT: Hibernate Session to User Session
>
>I was reading in the Hibernate forums that when you have a WebApp > DAO
>  
>
>>Hibernate > DB design, and you open/close each Hibernate session at
>>    
>>
>every single DAO method call (what I do now) its actually quite
>expensive, and is suggested that you attempt to maintain a Hibernate
>Session in conjunction with a user Session to increase performance.
>
>I had a question for anyone that has done this (without using
>SpringFramework). My gut-reaction to this was to add a
>HttpSessionListener to my webapp that created and stored a Hibernate
>session in the user's session, and then close it when the Session
>expired... will this not work? Anyone else have a good solution for
>this?
>
>TIA,
>Riyad
>
>
>---------------------------------------------------------------------
>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
>
>  
>

---------------------------------------------------------------------
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


Re: Hibernate Session to User Session

Posted by Riyad Kalla <rs...@email.arizona.edu>.
Marco,
I believe that is what I am using now (it seems to be quite popular) but 
I don't quite understand the reasoning behind the ThreadLocal approach 
(I've actually never used 'ThreadLocal')... can you shed some light on 
this? What the mapping between Hibernate Sessions and Users (or Threads) 
becomes?

Marco Mistroni wrote:

>Hi,
>	On hibernate website, there's an example that associates
>A session to a threadlocal. You can safely use that pattern (it's one
>Of hibernate patterns)...
>You could have a DAOFactory (plugin) which instantiates the
>SessionFactory,
>And each DAO will get its own session associated with its ThreadLocal..
>
>Have a look at hibernate website for patterns on how to use Session
>
>Regards,	marco
>
>-----Original Message-----
>From: Riyad Kalla [mailto:rsk@email.arizona.edu] 
>Sent: 08 June 2004 00:14
>To: Struts Users Mailing List
>Subject: OT: Hibernate Session to User Session
>
>I was reading in the Hibernate forums that when you have a WebApp > DAO
>  
>
>>Hibernate > DB design, and you open/close each Hibernate session at
>>    
>>
>every single DAO method call (what I do now) its actually quite
>expensive, and is suggested that you attempt to maintain a Hibernate
>Session in conjunction with a user Session to increase performance.
>
>I had a question for anyone that has done this (without using
>SpringFramework). My gut-reaction to this was to add a
>HttpSessionListener to my webapp that created and stored a Hibernate
>session in the user's session, and then close it when the Session
>expired... will this not work? Anyone else have a good solution for
>this?
>
>TIA,
>Riyad
>
>
>---------------------------------------------------------------------
>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
>
>  
>

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


RE: Hibernate Session to User Session

Posted by Marco Mistroni <mm...@waersystems.com>.
Hi,
	On hibernate website, there's an example that associates
A session to a threadlocal. You can safely use that pattern (it's one
Of hibernate patterns)...
You could have a DAOFactory (plugin) which instantiates the
SessionFactory,
And each DAO will get its own session associated with its ThreadLocal..

Have a look at hibernate website for patterns on how to use Session

Regards,	marco

-----Original Message-----
From: Riyad Kalla [mailto:rsk@email.arizona.edu] 
Sent: 08 June 2004 00:14
To: Struts Users Mailing List
Subject: OT: Hibernate Session to User Session

I was reading in the Hibernate forums that when you have a WebApp > DAO
> Hibernate > DB design, and you open/close each Hibernate session at
every single DAO method call (what I do now) its actually quite
expensive, and is suggested that you attempt to maintain a Hibernate
Session in conjunction with a user Session to increase performance.

I had a question for anyone that has done this (without using
SpringFramework). My gut-reaction to this was to add a
HttpSessionListener to my webapp that created and stored a Hibernate
session in the user's session, and then close it when the Session
expired... will this not work? Anyone else have a good solution for
this?

TIA,
Riyad


---------------------------------------------------------------------
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