title: Why the EntityManager is closed even if transaction has been committed?
body: <p>I am making some simple tests with JPA and Hibernate and I happen to have the EntityManager closed when I don't expect to.</p>
  
  <p>Here's my test main: </p>
  
  <pre><code>public class TestMain {
      public static void main(String[] args) {
          DBResourcesManager.initPeristence();
          List&lt;Event&gt; allEvents = EventDao.findAllEventsA();
          Event e1 = new Event("Event", new Date());
          EventDao.store(e1);
          DBResourcesManager.shutdown();
      }
  }
  </code></pre>
  
  <p>DB helper:</p>
  
  <pre><code>public class DBResourcesManager {
  
      private static EntityManager entityManager;
      private static EntityManagerFactory entityManagerFactory;
  
      public static void initPeristence() {
          entityManagerFactory = Persistence.createEntityManagerFactory("pu");
          entityManager = entityManagerFactory.createEntityManager();
      }
  
      public static EntityManager getEntityManager() {
          return entityManager;
      }
  
      public static void shutdown() {
          entityManager.close();
          entityManagerFactory.close();
      }
  }
  </code></pre>
  
  <p>Entity and corresponding DAO:</p>
  
  <pre><code>public class Event {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      private String title;
  
      @Temporal(TemporalType.DATE)
      private Date date;
  /*...*/
  }
  
  public class EventDao {
      public static void store(Event e) {
          EntityManager em = DBResourcesManager.getEntityManager();
          EntityTransaction t = em.getTransaction();
          t.begin();
          em.persist(e);
          t.commit();
          em.close();
      }
  
      public static List&lt;Event&gt; findAllEventsA() {
          EntityManager em = DBResourcesManager.getEntityManager();
          EntityTransaction t = em.getTransaction();
          t.begin();
          List&lt;Event&gt; events = em.createQuery("from Event", Event.class)
                  .getResultList();
          t.commit();
          return events;
      }
  }
  </code></pre>
  
  <p>In store, when beginning a transaction, I get the following:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: EntityManager is closed
  </code></pre>
  
  <p>The shutdown method is not executed.
  Why is the entity manager closed?</p>
  
  <p>For the sake of completeness, here's the persistence.xml:</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0"&gt;
      &lt;persistence-unit name="pu"&gt;
          &lt;class&gt;com.lh.hibernateexample.Event&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="hibernate.connection.url"
                  value="jdbc:mysql://localhost:3306/myfirsthibernatedb" /&gt;
              &lt;property name="hibernate.connection.username" value="XXXXXXXX" /&gt;
              &lt;property name="hibernate.connection.password" value="XXXXXXXX" /&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
              &lt;property name="hibernate.archive.autodetection" value="class" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>