I am using a Service Locator implementation which caches the result of javax.naming.Context#lookup
call, and maps it to the requested EJB interface, so all subsequent requests (for the same EJB) after the first one, return the cached instance.
My concerns are:
- Since the same instance is used, there is no utilization of the server EJB pool which would serve multiple simultaneous requests with multiple EJBs (unless the underlying server logic somehow makes use of the EJB pooling)
- Stateless and stateful EJBs are thread safe, but since, again, only one instance per EJB class is used, and EJB has EntityManager injected via @PersistenceContext, I assume that means that multiple threads could be using the same EntityManager instance (not just the persistence context), which definitely is not thread-safe
Do you think that it's best not to use caching in the Service Locator, or that my concerns are unjustified regarding EJB behavior?