I am learning JPA and the general pattern in examples seems to be as follows:
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
// ....
em.getTransaction().commit();
em.close();
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?