title: Getting a reference to EntityManager in Java EE applications using CDI
body: <p>I'm using Java EE 7. I would like to know what is the proper way to inject a JPA <code>EntityManager</code> into an <strong>application scoped</strong> CDI bean. You can't just inject it using <code>@PersistanceContext</code> annotation, because <code>EntityManager</code> instances are not thread safe. Let's assume that we want our <code>EntityManager</code> to be created at the beginnig of every HTTP request processing and closed after the HTTP request is processed. Two options come into my mind:</p>
  
  <p>1.
  Create a request scoped CDI bean which has a reference to an <code>EntityManager</code> and then inject the bean into an application scoped CDI bean.</p>
  
  <pre><code>import javax.enterprise.context.RequestScoped;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  @RequestScoped
  public class RequestScopedBean {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      public EntityManager getEntityManager() {
          return entityManager;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import javax.enterprise.context.ApplicationScoped;
  import javax.inject.Inject;
  
  @ApplicationScoped
  public class ApplicationScopedBean {
  
      @Inject
      private RequestScopedBean requestScopedBean;
  
      public void persistEntity(Object entity) {
          requestScopedBean.getEntityManager().persist(entity);
      }
  }
  </code></pre>
  
  <p>In this example an <code>EntityManager</code> will be created when the <code>RequestScopedBean</code> is created, and will be closed when the <code>RequestScopedBean</code> is destroyed. Now I could move the injection to some abstract class to remove it from the <code>ApplicationScopedBean</code>.</p>
  
  <p>2.
  Create a producer that produces instances of <code>EntityManager</code>, and then inject the <code>EntityManager</code> instance into an application scoped CDI bean.</p>
  
  <pre><code>import javax.enterprise.context.RequestScoped;
  import javax.enterprise.inject.Produces;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  public class EntityManagerProducer {
  
      @PersistenceContext
      @Produces
      @RequestScoped
      private EntityManager entityManager;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import javax.enterprise.context.ApplicationScoped;
  import javax.inject.Inject;
  import javax.persistence.EntityManager;
  
  @ApplicationScoped
  public class ApplicationScopedBean {
  
      @Inject
      private EntityManager entityManager;
  
      public void persistEntity(Object entity) {
          entityManager.persist(entity);
      }
  }
  </code></pre>
  
  <p>In this example we will also have an <code>EntityManager</code> which is created every HTTP request, but what about <strong>closing</strong> the <code>EntityManager</code>? Will it also be closed after the HTTP request is processed? I know that the <code>@PersistanceContext</code> annotation injects container-managed <code>EntityManager</code>. This means that an <code>EntityManager</code> will be closed when a client bean is destroyed. What is a client bean in this situation? Is it the <code>ApplicationScopedBean</code>, which will never be destroyed until the application stops, or is it the <code>EntityManagerProducer</code>? Any advices?</p>
  
  <p>I know I could use a stateless EJB instead of application scoped bean and then just inject <code>EntityManager</code> by <code>@PersistanceContext</code> annotation, but that's not the point :)</p>