-->
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.  [ 6 posts ] 
Author Message
 Post subject: thread local patern
PostPosted: Fri Aug 11, 2006 11:43 am 
Newbie

Joined: Thu Aug 10, 2006 12:02 pm
Posts: 3
Hi everybody,

is it possible to safely use this patern with lazy initializing?

Consider the following:

1. Servlet application (Apache Tomcat).
2. one session per thread (using ThreadLocal class)
3. one session per request (I use a servlet filter to close the session).
4. one collection can be associated with one session - attempt to associate a collection with two open sessions will throw an exception (org.hibernate.HibernateException).

Now consider a JSP page, which needs to load an object with a collection. If you try to load the same object (with the same collection) in two threads, you probably get the exception.

My guess is, that if two users tryes to load the page in the same moment, Tomcat try to serve the two requests simultaneously in two threads and one of these users get the HibernateException.

Am I right?

If so, is there any way of using several sessions in Tomcat? I know only one solution - to have only one session and synchronize requests - but I consider it to be really a poor solution..

Many thanks

Regards
Tony


Last edited by phobos on Sat Aug 12, 2006 8:02 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 2:07 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Each HTTP request will spawn it's own thread so ThreadLocal will work fine. The only time you would have a problem is when you attempt to load the same graph in the same Session, nothing to do with the thread, since each thread should have it's own ThreadLocal session.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 12, 2006 8:01 am 
Newbie

Joined: Thu Aug 10, 2006 12:02 pm
Posts: 3
I'm afraid you don't understand the problem.

Every thread has it's own session. But the problem occurs, when you try to load the same object to two threads (=two sessions).

So, the question is, whether is it possible to load a collection, which is already associated with another session...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 12, 2006 8:10 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Hmm. Maybe I'm being a little dense here, but there is no restriction or interaction between two separate and distinct sessions. The only problem you may encounter is if user A and user B both load the same object, make changes and then attempt to save. Then you would have to handle that concurrency issue in code, see http://www.hibernate.org/hib_docs/v3/re ... ctions.htm for info on that.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 12, 2006 12:18 pm 
Newbie

Joined: Thu Aug 10, 2006 12:02 pm
Posts: 3
Many thakns.

After reading the chapter and writing some test-code I think, that I understand the topic a little :o)

Supriselly, I'm now not able to write a code, which thros the Exception:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

-- which is, what I initially tryed to figure out.

Do you know, in which configuration this exception occures?

Regads
Tony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 9:12 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I usually see this message when someone forgets to close a session. Especially prevalent in the Open Session in View "pattern".


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