If a method of an EJB makes a JNDI lookup to the same EJB and then executes some other method, is this guaranteed to be executed in the same thread? How about the actual instance of the EJB object; is it the same instance as the one used in the calling method?
Share
The EJB specification makes no restriction as to the possibility of injecting an EJB into itself (see EJB 3.1 spec, chapter 16.5.1.1):
As with every EJB call, it will always be executed in the same thread; based on the Non-reentrant instances rule, it must not be the same instance (chapter 4.10.13):
This means: if bean A invokes a method on bean B, and B invokes any method on A, the container has to make sure that B calls another instance of A; the fact that it’s the same thread doesn’t matter, as reentrancy and thread-safety are different things.
The same situation applies when A calls itself through a looked-up JNDI reference, as container is involved at runtime in looking for a free instance of A. Conversely, if A directly invokes one of its methods, it’s a simple Java method invocation on “this”, not involving the container.