title: How to manage EntityManager life-cycle in CDI environment (using Tomcat)
body: <p>I am developing one application and I have started to use <code>CDI</code> along with <code>JSF</code> and <code>JPA</code>. The web container is <code>Tomcat</code>. </p>
  
  <p>I am very confused about <code>EntityManager</code> life-cycle in my <code>CDI</code> beans and I would need a good advise to clear some stuff in my mind.
  Generally what I've read is that <code>EntityManager</code> should be used mainly in a <code>Java EE</code> container, injecting it using <code>PersistenceContext</code> annotation. So then the container takes care about its life. However, if you do not use <code>Java EE</code> container(as <code>Tomcat</code>), then I need to manage my <code>EntityManager</code>'s life.</p>
  
  <p>Which are my best options now, using <code>Tomcat, CDI, JSF and JPA</code>?
  What I am currently doing now is the following: </p>
  
  <pre><code>public class EntityManagerFactoryProducer {
  
      public static final String TEST = "test";
  
      @Produces
      @ApplicationScoped
      public EntityManagerFactory create() {
          return Persistence.createEntityManagerFactory(TEST);
      }
  
      public void destroy(@Disposes
      EntityManagerFactory factory) {
          factory.close();
      }
  }
  
  public class EntityManagerProducer {
  
      @Inject
      private transient Logger logger;
  
      @Inject
      private EntityManagerFactory emf;
  
      @Produces
      public EntityManager create() {
          return emf.createEntityManager();
      }
  
      public void destroy(@Disposes
      EntityManager em) {
          em.close();
          logger.debug(String.format("%s Entity manager was closed", em));
      }
  }
  
  @Named
  @ViewScoped
  @Interceptors(LoggingInterceptor.class)
  public class ProductBacking implements Serializable {
  
      @Inject
      private ProductDAO productDAO;
  </code></pre>
  
  <hr>
  
  <pre><code>@ViewScoped
  public class ProductDAOImpl implements ProductDAO, Serializable {
      private static final long serialVersionUID = 4806788420578024259L;
  
      private static final int MAX_RANDOMIZED_ELEMENTS = 3000;
  
      @Inject
      private transient Logger logger;
  
      @Inject
      private EntityManager entityManager;
  
      @Override
      public List&lt;Product&gt; getSuggestedProducts() {
          logger.debug(String.format("%s Entity manager get products", entityManager));
  
          return entityManager.createQuery("SELECT p FROM Product p ORDER BY random()", Product.class).setMaxResults(
                  MAX_RANDOMIZED_ELEMENTS).getResultList();
      }
  
      @Override
      public void saveProduct(Product product) {
          logger.debug(String.format("%s Entity manager save product", entityManager));
  
          entityManager.getTransaction().begin();
          entityManager.merge(product);
          entityManager.getTransaction().commit();
      }
  
      @PreDestroy
      void destroy() {
          entityManager.close();
      }
  }
  </code></pre>
  
  <p>So basically I am just using plain <code>CDI</code> to accomplish this.
  However, I am not sure if this is standard way of doing it, and what is more important, I do not know how to close the <code>EntityManager</code> after bean life is over.
  As as summary: I end up with many unclosed connections (<code>EntityManager</code>s), so memory leak.</p>
  
  <p>Can someone help me understand how should I proceed?</p>