jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Advantage of Threadlocal over session object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Advantage of Threadlocal over session object " Watch "Advantage of Threadlocal over session object " New topic
Author

Advantage of Threadlocal over session object

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
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 ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
Phani Raju
Greenhorn

Joined: Aug 03, 2007
Posts: 19
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41132
    
  45
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.


Ping & DNS - my free Android networking tools app
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Apr 02, 2008
Posts: 576
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

Joined: Mar 06, 2001
Posts: 13459

because they know more than we do
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
 
Consider Paul's rocket mass heater.
 
subject: Advantage of Threadlocal over session object
 
Similar Threads
Jboss connection pooling timeout
is ThreadLocal devil in clustering environment?
Thread local
Hibernate transactions
why we define Session as ThreadLocal in hibernate