1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply..how do i make sure that session cache does not grow ?
Thanks again for giving the hint of clearing the session cache. I am able to recover from the out of memory error after using session.evict()
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.