title: Keeping JPA EntityManager open?
body: <p>I am learning JPA and the general pattern in examples seems to be as follows:</p>
  
  <pre><code>EntityManager em = factory.createEntityManager();
  em.getTransaction().begin();
  // ....
  em.getTransaction().commit();
  em.close();
  </code></pre>
  
  <p>Now I am wondering why do we continually create and close EntityManagers, as opposed to keeping it open and just starting new transactions? What are the benefits and costs of keeping it open vs closing it all the time?</p>