• Post Reply Bookmark Topic Watch Topic
  • New Topic

InheritableThreadLocal caches the value for all threads!  RSS feed

 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
We have a InheritableThreadLocal in which we set an object. This object contains some information so-called a request header and comes from webservice request. The thing is once we set the request header in first webservice method call, in other calls the thread local contains the last value set !!! and this is completely against the goal of defining thread locals

Can anyone help me on this?

here is the code to set the req header:
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just found that the problem is the thread pool of the app server. App server reuses threads so if the first webservice call uses THREAD-1 and then another method call comes from the client if THREAD-1 is idle the app server will use it so the previous value of InheritableThreadLocal will be used


Anyone knows how to tackle this problem?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Norouzi wrote:I've just found that the problem is the thread pool of the app server. App server reuses threads so if the first webservice call uses THREAD-1 and then another method call comes from the client if THREAD-1 is idle the app server will use it so the previous value of InheritableThreadLocal will be used


Anyone knows how to tackle this problem?


Don't use ThreadLocals to store information in web apps. Use Sessions.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve LukeDon't use ThreadLocals to store information in web apps. Use Sessions.[/quote wrote:

Thanks Steve, this is not a web app. Actually this is an ejb module to which clients are being serviced using both remote call and webservices. Using webservices we have no session since we can't have stateful webservices

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woops, sorry. Then if you aren't trying to store info in a state-full manner, there probably is no reason to store any value outside of local methods or values passed as method parameters. Does the web service maintain a Request scope? If not you could make one (a Wrapper around a Map perhaps) that you create early in the request life-cycle, kill late in the cycle, and make available through a method parameter to all intervening methods.


You could use ThreadLocal objects but you would have to remember to destroy them at the end of every request, or make sure they are empty at the start of every request (the latter is probably safer since the former might fail if the request ends in an error state unless you coded around that). I am not sure of EJBs, but is there some request listener or filter you could use that will be executed at the start of each request cycle to empty any old values in the ThreadLocal?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!