- I know that
EntityManager
instantiated by myself is not thread-safe. - I know that
EntityManager
injected via@PersistenceContext
in EJB behaves like hread-safe, because EJB container serializes access to EJB beans. - I know that in EJB injected
EntityManager
is really a proxy. - I know that when I inject
EntityManager
via@PersistenceContext
to@Dependent
CDI bean and inject that bean to EJB, it behaves like thread-safe because of covering EJB (I believe that in this caseEntityManager
is proxy too).
But:
- What happens when I inject
EntityManager
via@PersistenceContext
to CDI bean and use this bean directly for example in Servlet? I believe that thisEntityManager
is a proxy, so does this proxy guarantee thread safety?
Edit: Similar question Java CDI @PersistenceContext and thread safety does't solve my problem, because accepted answer shows EJB examples, not CDI examples.
Edit: I checked source code of WildFly application server and it looks that WildFly uses thread-safe proxy in CDI. This proxy selects real EntityManager when needed. Real EntityManagers are kept in special structure - stack of maps of EntityManagers in ThreadLocal.