2
  • 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 case EntityManager 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 this EntityManager 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.

guest
  • 1,696
  • 4
  • 20
  • 31
  • 1
    Possible duplicate of [Java CDI @PersistenceContext and thread safety](https://stackoverflow.com/questions/11063567/java-cdi-persistencecontext-and-thread-safety) – JGlass Oct 01 '18 at 17:55
  • See if that link helps you out! – JGlass Oct 01 '18 at 17:56
  • @JGlass, this link doesn't solve me problem, because accepted answer shows EJB examples, not CDI examples. – guest Oct 01 '18 at 18:07
  • They're making it seem that regardless of EJB or CDI - it's not thread safe. The OP on the other question did state the same question you did - I think they're example are just EJB related and not CDI but are still relevant? – JGlass Oct 01 '18 at 18:24
  • No, CDI is different story than EJB. – guest Oct 01 '18 at 18:27
  • In CDI, all normal scoped beans use proxy. In short that mean any standard scope excepting `@Dependent` will be proxied. That being said, I don't see how proxy alone could imply thread-safety. In my opinion you need to add some extra code for that in your CDI bean. – Siliarus Oct 02 '18 at 07:59
  • @Siliarus but this injected EntityManager is not CDI proxy, but different proxy. A lot of frameworks have a concept of proxy. – guest Oct 02 '18 at 08:24
  • Yea I know that - misread your question. Then it depends wholly on the JPA providing the proxy and has very little to do with the CDI bean you use. It might even depend on JPA implementation you are using, might be worth mentioning. – Siliarus Oct 02 '18 at 08:32
  • @Siliarus, generally I tend to believe that this depends on application server, because I was reading various Java EE specifications and didn't find answer, but I would like to see answer from some authority. – guest Oct 02 '18 at 13:48
  • I find two resources that say Transaction Scoped Entity Manager is a stateless bean(session bean) so it is thread-safe. I have updated my post. – Mehran Mastcheshmi Nov 28 '18 at 15:02

1 Answers1

1

https://www.javacodegeeks.com/2013/06/jpa-2-entitymanagers-transactions-and-everything-around-it.html says:

The biggest benefit of using Transaction Scoped Entity Manager is that it is stateless. This also makes the Transaction Scoped EntityManager threadsafe and thus virtually maintenance free

also pro JPA book says:

a transaction-scoped entity manager is stateless, meaning that it can be safely stored on any Java EE component

Mehran Mastcheshmi
  • 785
  • 1
  • 4
  • 12