guest
guest

Reputation: 1746

Is EntityManager obtained via @PersistenceContext in CDI in Java EE application server thread-safe?

But:

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.

Upvotes: 2

Views: 672

Answers (1)

Mehran Mastcheshmi
Mehran Mastcheshmi

Reputation: 815

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

Upvotes: 1

Related Questions