|
I found the following behaviour incorrect, in the following scenario:
MyEntity entity = new MyEntity();
EntityManager em = entityManagerFactory.createEntityManager();
try {
em.getTransaction().begin(); em.merge(entity); em.getTransaction().close();} catch (Exception e) {
try {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} catch (Exception excRollback) {
logger.error(excRollback);
} finally {
try {
em.close();
} catch (Exception excClose) {
logger.error(excClose);
}
logger.error("EntityManager isOpen: " + em.isOpen());
}
}
By "Exception M" I mean the exception caused, when the DB connection is lost (I debugged it by killing the connection from the MySql console).
There are three scenarios here, depending when the exception occurs, but the definitely worng behaviour is "Use Case 2", and namely when `em.close()` is called. On that call, an exception is thrown and the `em.isOpen()` still returns true. But according to the documentation of the [EntityManager.close()] the entityManager should return false when asked if `isOpen()` (even if the connection is already closed).
PS: to write a test-case is not so easy, as it is not so easy to controll when the connection gets killed.
|