-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Session, and thread-safety in webapps with ThreadLocal
PostPosted: Fri Apr 23, 2004 12:50 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Hello,

While testing my webapp with Jmeter I realized my app has thread-safety issues, because of how I use Hibernate Session.

I use the 'open in Servlet Filter, put in ThreadLocal, assign to User, execute, close in Filter' approach.

The point is that each user has its own Session. Multiple users can access the app simultaneously - there are no thread issues there.

The problem shows if the same user accesses the app from multiple threads (think load testing with a single user, or a user who rapidly opens multiple app pages in several browser tabs).
What happens is that several transactions use the same Session, and since the Session is blindly closed in the Filter after each HTTP request, it is possible that the second transaction ends up trying to use a Session that has already been closed.

How do people deal with this type of situation?
Perhaps I could check/force Session re-connect right before each transaction, but that's messy, isn't it?

Thanks.
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
1 Hibernate session per HttpRequest
1 HttpRequest per HttpRequest thread
a transaction "belongs" to a connexion

So if a transaction tries to commit on a closed connexion... it means your transaction management is somewhere unsecure.

Have you noticed exactly which code is wrong? if yes show us


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:33 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
delpouve wrote:
1 Hibernate session per HttpRequest


Woah! Really? Why? The problem is when the same user hits the app with multiple threads/requests in parallel. I don't think it makes sense to give each of those threads a new, separate, Hibernate Session, does it?

Thanks,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:37 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
of course it does since Hibernate session is NOT threadsafe

what you can do if you want to continue like this is to add a servlet filter which allows only one concurrent httpRequest per session... I mean the second request will "wait" the first to be ended before being listening by the server


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:38 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
put in ThreadLocal, assign to User


assign to user ? what does it mean exactly? put it on httpSession?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 2:08 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
delpouve wrote:
Quote:
put in ThreadLocal, assign to User


assign to user ? what does it mean exactly? put it on httpSession?


No, not HttpSession.
It just means that in the code that handles user requests and Hibernate Sessions keeps a mapping: User -> Hibernate Session.
That way, when a user makes a request, I first try to get Hibernate Session from there and call reconnect() on it. This way, I benefit from Hibernate Session acting as a cache, and I don't have to re-create new Hibernate Sessions for users that repeatedly request pages.


Regarding your comment above - I think you are right. I would have to either create multiple Hibernate Sessions for each user's thread. And I could queue up requests as you suggested above. Or, I could just declare my webapp not thread-safe for individual users making simultaneous requests. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 2:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i advice you to keep exactly 1 session per httpRequest (same user or not)
session is unexpensive and you are mistaken about "cache" aspect.
Try EHCache for second level cache, it's easy to configure.

And session.reconnect is to use if session has been disconnected not closed[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 2:32 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
delpouve wrote:
session is unexpensive and you are mistaken about "cache" aspect.
Try EHCache for second level cache, it's easy to configure.

And session.reconnect is to use if session has been disconnected not closed[/b]



Making a new Hibernate Session is inexpensive. However, once I start data that I already accessed, I would have to re-fetch it from DB if I were to use a new Session every time. That, I though, was the first level of cache - Hibernate Session 'remembering' data about previously loaded entities.
No?
Yeah, I do use session.reconnect() and session.disconnect().

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 2:33 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you want to keep loaded objects, read about detached and reattach in the doc


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 24, 2004 2:38 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
otis wrote:

Making a new Hibernate Session is inexpensive. However, once I start data that I already accessed, I would have to re-fetch it from DB if I were to use a new Session every time. That, I though, was the first level of cache - Hibernate Session 'remembering' data about previously loaded entities.
No?
Yeah, I do use session.reconnect() and session.disconnect().

Thanks!


"session.reconnect() " is an anitipattern for many reasons.
It is possible to make this way thread safe with some fake attribute in httpsession and Filter, but I see no way to manage resources, your user becomes resource manager on server.

The most simple scalable way is to create session per request, but you will lose
performance. Workaround for performance is cache, a global object cache (independant on user count), content cache, query cache ("materialized view"),
indexes and configuration parameters.

There are more ways to increase performance than scalability and it is better to think about scalability first, it depends on application architecture.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 24, 2004 2:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
"session.reconnect() " is an anitipattern for many reasons.


I completely disagree.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 24, 2004 8:31 pm 
Regular
Regular

Joined: Tue Sep 30, 2003 11:27 am
Posts: 60
Location: Columbus, OH, USA
gavin wrote:
Quote:
"session.reconnect() " is an anitipattern for many reasons.


I completely disagree.


I would propose that neither of these responses are useful, but I for one would be interested in more info. My app uses one Hibernate session per HttpRequest, as well as a 2nd-level cache provided by ehcache, but I'm always looking for better ways of doing things.
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 4:36 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
but I'm always looking for better ways of doing things

but what?

Gavin and Baliukas always have interesting things to say, what is the problem? We are on a forum... where people can help and talk... keep cool.

Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 6:42 pm 
Regular
Regular

Joined: Tue Sep 30, 2003 11:27 am
Posts: 60
Location: Columbus, OH, USA
delpouve wrote:
Quote:
but I'm always looking for better ways of doing things

but what?

Gavin and Baliukas always have interesting things to say, what is the problem? We are on a forum... where people can help and talk... keep cool.

Anthony


Hmm, maybe something got lost in translation - I'm not hot at all. In fact, my eyes and ears are wide open for any good recommendations about how to manage Hibernate sessions in a web app. Gavin disagreed that session.reconnect() is an antipattern, but he didn't expand on it. Furthermore, most every forum topic I've read (and believe me, I've searched) and every wiki article I've studied seems to point to one Hibernate session per HttpRequest using a servlet filter as a best practice. Yet Gavin's line about session.reconnect() makes me wonder if there's a secret chef's recipe worth learning more about.

Gavin, I know you're very busy, but could you quickly point me to a URL or something to help me understand your perspective on optimal use of Hibernate sessions in a web app? Thanks in advance!
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 2:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://forum.hibernate.org/viewtopic.php?t=930018

there is a use case using reconnect explained


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.