jpajakarta-eethread-safetycdi

Is EntityManager injected with @PersistenceContext to a @RequestScoped CDI bean thread-safe?


In a Java EE web application project, there is a DAO annotated as a CDI bean:

@RequestScoped
public class CustomerDAO {
  @PersistentContext
  private EntityManager em;
  //some persistence operation afterwards
  @Transactional
  public void update() {
  //implementation using the injected em
  }
}

The injected EntityManager is not thread-safe according to JPA spec, but the Question is:

Is the injection of EntityManager into this per request @RequestScoped CDI bean thread-safe? and if it is not thread-safe, what potential concurrency issues there might be?


Solution

  • Nothing is shared across threads if you think about it, entity manager is injected in request scope and released after request is done but if you have concurrency doubts use Multiversion concurrency control.