When should I close connection with database ? I create connection one time when application starting, and then I use entity manager in this way:
public void createItem(TYPE item){
em.getTransaction().begin();
em.persist(item);
em.getTransaction().commit();
}
public class Connection { //creating connection (one time)
private static final String PERSISTENCE_UNIT_NAME = "ejb";
private static EntityManagerFactory factory;
private static EntityManager em;
static{
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = factory.createEntityManager();
}
public static EntityManager getEntityManager() {
return em;
}
}
I think it does not make sense to close connection evey each operation (like create) and create new connecion when I want to save item again.