-->
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.  [ 3 posts ] 
Author Message
 Post subject: Dealing with Session thread-unsafety in web apps, HIA p.328
PostPosted: Wed Mar 30, 2005 12:04 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Hibernate version: 2.1*

Hello,

I have a webapp that uses Hibernate, and I'm considering saving Hibernate Session in HttpSession, so I can benefit from the Session acting as a second level cache and thus avoid a few DB hits. Unfortunately, Session is not thread-safe, so a user hitting my webapp with multiple parallel requests (e.g. quickly opening multiple pages in new tabs) is likely to cause a HibernateException.

Hibernate in Action mentions this problem on page 328 (middle of the page), but there is no "recipe" given. So I am wondering how others handle this problem.

I can insert a servlet filter in the request chain, and ensure that only 1 request is processed at a time, but I am wondering if there is a better, more elegant approach.

How do others deal with this?

Thank you,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 3:49 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It must be better to create hibernate session per request and to use "global" cache for optimization. Heavy HttpSession can produce scalability problems.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 3:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
I can insert a servlet filter in the request chain, and ensure that only 1 request is processed at a time, but I am wondering if there is a better, more elegant approach.

How do others deal with this?


this the right way to avoid concurrent httrequests.

Be sure to use a long session (storing it in httpSession) only if you really need it. This is called long-session-per-application-transaction pattern. As baliukas said, prefer short session with detached object if you need failover.

Even with long-session-per-application-transaction, you must take care of the session size and no consider it as a second level cache: it isn't

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.