简体   繁体   中英

EntityManager connections stuck at 'Idle in transaction' after persist/commit

My EntityManager is persisting/committing data to a Postgres database no problem. However, the connections it makes get stuck at 'Idle in transaction'. Here's my code:

public User create(User user) {

    if(logger.isDebugEnabled()) {
        logger.info("creating user: {}", user);
    }

    EntityManager entityManager = DbUtil.factory.createEntityManager();

    try {
        entityManager.getTransaction().begin();
        // Persist takes an entity instance, adds it to the context and makes that instance managed (ie future updates
        // to the entity will be tracked).
        entityManager.persist(user);
        entityManager.getTransaction().commit();
    }
    catch(RuntimeException e) {
        throw getDbException(e);
    }
    finally {
        entityManager.close();
    }

    return user;
}

Any idea why they aren't closing?

You say that persisting works, probably you see your data in db. But do you see all the data, weren't there failures? Your code doesn't handle transaction rollbacks, that might be the reason you see connection in 'Idle in transaction' state.

If an exception is thrown by throw getDbException(e) , the transaction is not rolled back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM