• 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

Advantage of Threadlocal over session object

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Advantage of Threadlocal over session object in web application. I can save all data related to particular user in Session object or Threadlocal ?.
[ October 15, 2008: Message edited by: jacob deiter ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not the same thing, since ThreadLocal is only valid for the duration of the request. You still need a session to maintain data between requests. The next request from the same user is unlikely to go to the same thread, so the ThreadLocal state will be either lost or invalid.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jacob deiter:
Advantage of Threadlocal over session object in web application. I can save all data related to particular user in Session object or Threadlocal ?.

[ October 15, 2008: Message edited by: jacob deiter ]



Both could be used as data holders, but are drastically different coming to visibility/scope.

ThreadLocal is specific to a thread - meaning whatever info you put in here is not visible/accessible from other threads. On the other hand data kept in a Session is accessible to all threads part of that session.

If you need to manage some data, which you can not have as local variables then ThreadLocal is the way to go.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ThreadLocal is only valid for the duration of the request.


Since we're talking about servlets here, any ThreadLocal would likely be an instance (or static) variable of a servlet, since it doesn't really make sense to have a ThreadLocal as a local variable in a method. In that case it could potentially live for the lifetime of a servlet, not just for one request. That would depend on how the servlet container uses threads and thread pools, though.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
thank for reply.I agree that threadlocal for a thread but I will make a threadlocal available for all threads from same user. My intention is as follow

1)when a new user come into the application ,I will create a threadlocal variable and place all service layer class into that

OR

2) I should use session object to place all service object
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"service object"? What are these? What is is you want to cache and why?

Another issue with using a ThreadLocal to store stuff in a servlet is how this would behave in a cluster. JEE containers are required to provide session replication between nodes, but no simmilar requirement exists for ThreadLocals.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I advise against using ThreadLocal. I believe there are alternate effects or gotchas that you are not expecting and would be better off avoiding.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
upto my knowledge,Hibernate use Threadlocal for maintaining its session.if threadlocal if not advisable then why hibernate use this
[ October 15, 2008: Message edited by: jacob deiter ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because they know more than we do
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

upto my knowledge,Hibernate use Threadlocal for maintaining its session.



One second, a Hibernate session is not a HTTP session. It is 'common' to use ThreadLocal to manage connections and transaction boundaries, and my belief is that this what Hibernate will be doing.

Note that I say 'common' since I would still consider this 'advanced programming' and not a practice to follow just because we feel like it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic