-->
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: Is ThreadLocal pattern *really* thread safe?
PostPosted: Thu Mar 02, 2006 8:50 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
I'm trying to get my head around the ThreadLocal pattern (although I can't find it anymore in the Wiki) and I would really appreciate your help. I did have a read through the forum but I couldn't really find an answer.

Lets see the standard getSession() method of the pattern

Code:
Session s = (Session) threadSession.get();
if (s == null) {
   s = getSessionFactory().openSession();
   threadSession.set(s);
}

return s;   


Isn't it possible that two threads would get two different Session objects since the block isn't synchronized?

Seeing as one thread comes around, finds Session to be null tries to acquire a new one, at that time a second one comes around and since the first one hasn't really assigned the Session to the Threadlocal, gets null as well.

So the two threads end up with 2 sessions out of which only 1 of them gets closed when calling the closeSession().

Code:
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
   log.debug("Closing Session of this thread.");
        s.close();
}


Thanks in advance.

_________________
eu:life
http://www.eulife.gr


Last edited by Cue on Thu Mar 02, 2006 9:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:18 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I suppose you are using Hibernate 2 as in Hibernate 3 you can let the sessionFactory bind the session to the current thread automatically.

In my opionion you are correct. I will check this in the SessionFactory implementation to see if this could not be a problem there.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:42 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
As I was reading through this I realized that the following statement is false

Quote:
So the two threads end up with 2 sessions out of which only 1 of them gets closed when calling the closeSession().


since each thread will get its associated Session and close it.

My confusion actually started when I saw the "reference counting" implementation of the ThreadLocal.

Code:
Session s = (Session) threadSession.get();
if (s == null) {
   s = getSessionFactory().openSession();
   threadSession.set(s);
   level++;
}

return s; 


Why is level incremented for all threads and not per thread using a ThreadLocal as well? :/

_________________
eu:life
http://www.eulife.gr


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.