Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ThreadLocal question (to Mark)

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, thanks lot for your feedback for the questions I asked about using

private static ThreadLocal session;

I guess now I understand it better --- Do you mean that, although this session is a static variable, it is a ThreadLocal, so for example, for a servlet instance, when multiple servlet thread call this DAO class, each servlet thread gets a separate Hibernate session from the DAO class. So when one thread closes the session it doesn't affect other threads' sessions. If it is not defined as "ThreadLocal" then it can be a problem -- this static variable can be modified by any client and will affect others. Is this what you meant ?

In the test I did by myself, I created two classes that extend the DAO class, then I created two objects for these two classes, I found once one cobject closes its session, the other one gets null session value. I guess this is because they are not good example for threads. They are two separate objects. Is that right ?

Thanks.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES, 100% correct. That is exactly what I was saying.

The Javadocs for ThreadLocal is what I used to help me understand how it was working.

Mark
[ December 21, 2007: Message edited by: Mark Spritzler ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic