lam
lam

Reputation: 13

Hibernate - Java pagination - Out of memory exception

I am using the following piece of code to get results from DB as result set is huge.

Is it the correct way doing? I am running in to Out of memory exception and I suspect it is because of this.

// div is set to 10,000
// i have calculated how many times i need do it using a count query
// and that value is being used in iteration variable

Query bigQ=session.createSQLQuery(bigQuery);

for(int i=0;i<iteration;i++)    
{
    bigQ.setFetchSize(div);                         
    bigQ.setMaxResults(i*div);

    List<Object[]> result=bigQ.list();

    // now i am using the result to get the values 
    for(Object[] a:result)
    {
        // rest of operations
    }
}   

Note:

  1. I have set enough Xms and -Xmx in the jboss run.conf.
  2. I am not able use scrollable set as the postgre driver is not supporting it.
  3. I can not use createQuery as in that case hibernate is generating lots of queries, so i am using a big join query, along with createSQLQuery.

Upvotes: 1

Views: 1563

Answers (2)

axtavt
axtavt

Reputation: 242706

Your code looks strange - the correct way to paginate results would be this:

bigQ.setFirstResult(i * div);
bigQ.setMaxResults(div);

Also make sure that your session cache doesn't grow - if your query returns only scalar values (not entities) it shouldn't be a problem, but if you load some entities in // rest of operations, it can.

Upvotes: 2

Justin Thomas
Justin Thomas

Reputation: 5848

Use offset and limit... http://www.petefreitag.com/item/451.cfm

Upvotes: 0

Related Questions