• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ThreadLocal question (to Mark)

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic