• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

ThreadLocal - will it return the same value for a single user

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I want to use UserContext class in web and stand-alone applications. The application will have multiple users. I am using JDK 1.5 and WebSphere 6.1. For a given user, I set the userID by calling setUserId(String userId). I want to get this back this userID, when I call getUserID() method. However, I would like to get some assurance on my approach that it will work. I am yet to test it in a multiuser and clustered environment.

As every user logs in, I set userID of the user by calling UserContext.setUserId(String userId). For example, if user John logs in, I set his userID as "John". If user Peter logs in, I set his userID as "Peter". Obviously, userID set is not as simple as "John", or "Peter"; but some other kind of identifier. But this is an example.

My questions are:
1. When I call UserContext.getUserID() in the code, will it return "John" for user John and "Peter" for user Peter? My understanding is, ues; it will return "John" for user John and "Peter" for user Peter.

2. Can I call UserContext.getUserID() method in any part of the code to get the userID back? Or, do I a need to call UserContext.getUserID() ) in a particular part of the code and pass the retrieved user ID as argument to any method. My understanding is, I can call UserContext.getUserID() in any part of the code and it will correctly return "John" or "Peter".

I think my assumptions are correct.
Any comment or suggestion will be appreciated.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Gehouse wrote:My questions are:
1. When I call UserContext.getUserID() in the code, will it return "John" for user John and "Peter" for user Peter? My understanding is, ues; it will return "John" for user John and "Peter" for user Peter.

2. Can I call UserContext.getUserID() method in any part of the code to get the userID back? Or, do I a need to call UserContext.getUserID() ) in a particular part of the code and pass the retrieved user ID as argument to any method. My understanding is, I can call UserContext.getUserID() in any part of the code and it will correctly return "John" or "Peter".

I think my assumptions are correct.
Any comment or suggestion will be appreciated.



The answer to both is a qualified - yes.

Yes, when called from any where in your code it will return the same value you input... as long as the code is being executed in the same Thread that the set... method was called in. Once you change Threads, the value will change (which is why it is called ThreadLocal).

This becomes complicated in Web Applications if you expect the value to persist over multiple requests. Each request would have a different Thread, and so a different value for the UserID. They are often pooled as well, so "John" may get "Peter"'s ID a few requests down the road. It is also possible.
 
Oh. Hi guys! Look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic