Search code examples
javahibernatejpaguicetransactional

Error when retrying method after OptimisticLockException - org.postgresql.util.PSQLException: This statement has been closed


I have method:

public void changeItemName(long id, String nmae) {

    Item item = itemDAO.find(id);
    item.setName(name);

    try {
        itemDAO.save(item);
    } catch (RollbackException | OptimisticLockException | StaleStateException e) {
        logger.warn("Retry method after " + e.getClass().getName());
        itemDAO.clear();
        changeItemName(id, name);
    }
}

First, I manually provoke OptimisticLockException by setting higher version, so it goes to catch block, clears EntityManager and retries the method. When retrying, the object/entity is refreshed and has correct version but I get:

javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:86)

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not update: [com.example.Item#1]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)

Caused by: org.hibernate.exception.GenericJDBCException: could not update: [com.example.Item#1]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

Caused by: org.postgresql.util.PSQLException: This statement has been closed.
at org.postgresql.jdbc2.AbstractJdbc2Statement.checkClosed(AbstractJdbc2Statement.java:2653)

Database module (using Guice 4.1.0):

public class DbModule extends PrivateModule {

    @Override
    public void configure() {
        install(new JpaPersistModule("persistence-unit").properties(jpaProperties()));

        ...

        Key<PersistFilter> key = Key.get(PersistFilter.class, ExamplePersistenceUnit.class);
        bind(key).to(PersistFilter.class);
        expose(key);
}

Save method implementation (using Hibernate 5.1.0.Final):

@Inject
protected EntityManager entityManager;

@Override
public void save(T entity) {
    entityManager.getTransaction().begin();
    entityManager.persist(entity);
    entityManager.getTransaction().commit();
}

Why does it happen?

UPDATE

After some debugging, I have noticed that:

  • 1st method invocation - I get OptimisticLockException / StaleStateException - that's ok, it was intentionally raised and expected
  • 2nd method invocation, retry, I get Caused by: org.postgresql.util.PSQLException: This statement has been closed - this one was unexpected
  • 3rd method invocation, another retry - it works well and saves to the DB successfully

All 3 times, the same instance of EntityManager entityManager is used.


Solution

  • I also encountered the same issue, and with the help of @Jonathan's comment, I solved it by using the: @Retryable(include = {ObjectOptimisticLockingFailureException.class, JpaSystemException.class}, backoff=@Backoff(delay = 100))

    See also good Q&A: Spring @Retryable - how to log when it is invoked?