Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java persistence memory leaks

I have 1M rows in a table and I want to get all of them. But when I try to get all rows with jpa by pagination then I get java heap error. Do you think that am I missing something? Any advice

int counter = 0;
while (counter >= 0) {
   javax.persistence.EntityManager em = javax.persistence.Persistence
     .createEntityManagerFactory("MyPU")
     .createEntityManager();

   Query query = em.createQuery("select m from mytable m");
   java.util.Collection<MyEntity> data = query
          .setFirstResult(counter).setMaxResults(1000).getResultList();
   for(MyEntity yobj : data){
            System.out.println(obj);
   }
   counter += 1000;
   data.clear();
   em.clear();
   em.close();
}
like image 566
user704006 Avatar asked Apr 30 '26 21:04

user704006


1 Answers

Since you use native SQL anyway, can't you specify the LIMIT :counter, 1000 (or ROWNUM BETWEEN :counter AND 1000 if using Oracle) directly in your SQL statement?

like image 172
Lukas Eder Avatar answered May 03 '26 09:05

Lukas Eder