-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate session per servlet session or request
PostPosted: Mon Jan 05, 2004 7:44 pm 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
It seems to me there are two different ways to handle Hibernate sessions in a Servlet environment. The first one is to create a new session for every request (as in the reference documentation section 1.4). The other approach is to put the hibernate session in the servlet session, and then to reconnect and disconnect for every request (as mentioned in section 17.3.1).

Is the disconnect/reconnect only meant to be used for a single "business transaction" with user interaction?
What is otherwise the advantage/disadvantage of each approach?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 7:54 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
For a long time I tried holding on to the same Hibernate session in the HTTP session and connecting/disconnecting between requests. It was pretty much a disaster. :) I had lots of problems with stale Hibernate sessions and JDBC connections, even with JDBC pooling. When I started creating a new Hibernate session with each request I found no performance penalty. (The application I use it for is very low volume, though...)

So, my advice? Just let go. :)

What I'm doing now is holding a DataAccessWrapper (my own class) in the HTTP session. When a connection comes in I call the getDataAccessObject() method which creates an internal session, if necessary. I call the cleanupDataAccessObject() method when I'm done. (flushes and closes the session, then sets it to null.) I could easily shift back to maintaining a Hibernate session all of the time by changing the behavior of those methods. Try it and see which works best for you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 2:29 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It is very dangerous to store session in httpsession.
1. resources are wasted (memory and DB connection)
2. all objects stored in htpsession must be thread safe, session is not threadsafe.

BTW it is not a problem to close phisical connection at the end request too, if you do not open connection more than once per request.
some of my applications are more scalable without connection pools
on "big" machines (a few GB of memory, separate disks with optical interface for system, data and logs). Sometimes pooling intoduces more problems than optimizations.There is no optimization in some tests, but it depends on things like DB authentication configuration, "aggressive" cache and resource management in application.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 3:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
all objects stored in htpsession must be thread safe


Is this really necessary?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 3:27 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
gavin wrote:
Quote:
all objects stored in htpsession must be thread safe


Is this really necessary?


In theory it must, but it can work without problems in practice.
It can be a few concurent requests per session: frames, images, ... are sometimes dynamic (or can become dynamic later after UI changes),
some script can open a few windows too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.