Hello,
I try to close all connections and resources so as to avoid "lost" active database connections.
The code structure is:
Code:
javax.persistence.EntityManager entityManager = null;
javax.persistence.EntityTransaction transaction = null;
try {
entityManager = com.primari.business.PrimariUtility
.getEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
// Here I do all the CRUD stuff
transaction.commit();
} catch (RuntimeException e) {
if (transaction != null && transaction.isActive())
transaction.rollback();
throw e;
} finally {
entityManager.close();
}
"com.primari.business.PrimariUtility.getEntityManager()" has the following code:
Code:
public static EntityManager getEntityManager() {
EntityManagerFactory emFactory = Persistence
.createEntityManagerFactory("primall");
EntityManager entityManager = emFactory.createEntityManager();
return entityManager;
}
I really don't understand why after testing the application a few minutes Hibrernate thows a "Too man connections" exception.