Friday, December 19, 2008

Why Threadlocal musn't be used with EJB...

There is nothing in the EJB specifications that prohibits the use of ThreadLocal.
It only says in the Programming Restrictions (21.1.2):
The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread’s priority or name. The enterprise bean must not attempt to manage thread groups.

But nor does the EJB specifications ensure that all local invocations in the EJB container will run in the same thread.

Generally application servers use a single thread for local invocations: I have tested it on OC4J and JBoss.
But I think it's really dangerous to rely on this feature as it can change without warning.

So, my recommendation is "Dont't use ThreadLocal with EJB" !

No comments: