Affects: 2.3.3

If there is bad code, say that doesn't rollback or commit a transaction, when the Entity Manager tries to close, it will simply log the error, and in my case leaves a DB connection tied up with a bad state.

I am using JPA, along with accessing the DB connection to mix nested (NEW) transactions. And if the EntityManager close fails then the connection becomes unusable with the following error:

org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! JpaTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommen ded to use a single JpaTransactionManager for all transactions on a single DataSource, no matter whether JPA or JDBC access.

Here is the code in question:

/**
 * Close the given JPA EntityManager,
 * catching and logging any cleanup exceptions thrown.
 * @param em the JPA EntityManager to close (may be {@code null})
 * @see javax.persistence.EntityManager#close()
 */
public static void closeEntityManager(@Nullable EntityManager em) {
    if (em != null) {
        try {
            if (em.isOpen()) {
                em.close();
            }
        }
        catch (Throwable ex) {
            logger.error("Failed to release JPA EntityManager", ex);
        }
    }
}

So - my question is this: shouldn't this exception be thrown, and not just logged? Because it is not thrown there is no way to detect the method that forgot to commit/rollback the transaction. No way to recover.

I would think it would be much better to log, and then throw it.

Comment From: snicoll

The error above already states that your setup is not compliant so I'd fix that before doing anything else. There are many places in the framework when shutdown code are designed in such a way that we logger the underlying error and continue. This looks ok to me.