I've heard that many J2EE containers implement some form of thread pooling.
Depending on how this is implemented, the same session bean or servlet instance could run on a different thread each time a request is made, making ThreadLocal usage unsafe (please, take a look at http://sys-con.com/itsg/virtualcd/Java/archives/0711/goenka/).
But J2EE 1.3 and 1.4 spec doesn't prohibit ThreadLocal usage. So, what is the de facto standard?
You are right, there is no guarantee that a particular thread will be used by a particular ejb or servlet. So threadlocals should not be used. Even if container picks up the same thread to run the ejb, in a highly available/clustered environment, request may land up in different JVMs, so process wide synchronisation won't suffice.
Refer to section #25.1.2 of EJB 2.1 spec where some programming restrictions for EJB are mentioned. You may also refer to In J2EE platform spec where security permissions for different containers are mentioned.
Hi to all,
ThreadLocal is simply used for only naming to thread Object that'why ThreadLocals are implemented using a Map attached to each Thread instance, so their values will disappear when the Thread is no longer used and garbage collected.
And Garbage collector is dedicated to JVM.so you ca'nt depeond on this type of coding.