aspose file tools*
The moose likes Java in General and the fly likes ThreadLocal - will it return the same value for a single user Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ThreadLocal - will it return the same value for a single user" Watch "ThreadLocal - will it return the same value for a single user" New topic
Author

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

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281


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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


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