6

I am using JPA 2.1, Oracle DB and have a list of ids for entities to be removed (about 430000 ids). At first, it was implemented as splitting that id list into each smaller one with 1000 ids, pass them as parameters for a JPQL and executing.

delete from SOPFilter f where f.id in (?1)

Then, I want to change to use JPA CriteriaDelete.

CriteriaDelete<SOPFilter> criteriaDelete = cb.createCriteriaDelete(SOPFilter.class);
Root<SOPFilter> from = criteriaDelete.from(SOPFilter.class);
criteriaDelete.where(from.get(SOPFilter_.id).in(sopFilterIds));

It runs fine until it reach the 90000th one and there is a runtime exception cause it to stop here

org.hibernate.SessionException: Session is closed

and make entity manager factory to close.

INFO : bernate.impl.StmpContainerEntityManagerFactoryBean: Closing JPA EntityManagerFactory for persistence unit 'IMOFFERINGMANAGEMENT'

For whom was mislead by my first post with this exception

java.lang.IllegalStateException: EntityManagerFactory is closed

There was a catch clause to handle runtime exception by adding a record to database before throwing it. And to add a event record, it attempts to create another entity manger from the factory which is closed now.

public static void logEvent(EntityManager em) {
    EntityManager em2 = null;
    EntityManagerFactory emFactory = em.getEntityManagerFactory();
    em2 = emFactory.createEntityManager();
    // ...
}

Could anyone shed some light on it?

pirent
  • 83
  • 6
  • 1
    Give some more details how you are using JPA CriteriaDelete. Also as per your question you have about 43000 ids and exception got thrown when control reaches 90000th row? is that 9000th row you mean? – Ashish Patil Aug 16 '16 at 12:29
  • how can the EMF be closed? part of a finally block and some exception was thrown? guessing is all that people can do here with that info – Neil Stockton Aug 16 '16 at 12:34
  • Is it weird that I read the title in Donald Trump's voice? – shmosel Aug 17 '16 at 02:20

1 Answers1

0

Im not clear on your code but you are likely hitting a transaction timeout. You can set a hint -

query.setHint("javax.persistence.query.timeout", 8000);

There may also be timeouts on the database side

farrellmr
  • 1,815
  • 2
  • 15
  • 26