0

I read about the ScrollableResults in Hibernate for efficiently handling large data.

ScrollableResults results = session.createCriteria(Employee.class).scroll(); -- (1) 
while (results.next()) {  
    Object row = results.get(0);  
}

On running above, I found that select query is fired at statement (1) itself. Doesn't it mean that all the data from table is loaded into memory? If it is not the case, how does it work?

Can someone please explain.

Anand
  • 20,708
  • 48
  • 131
  • 198
  • 1
    http://www.numerati.com/2012/06/26/reading-large-result-sets-with-hibernate-and-mysql/ read it here – Zeus Dec 09 '14 at 21:00

1 Answers1

1

Yes it does load in memory. You need to have the code as below.

Query query = session.createQuery(query);
query.setReadOnly(true);
// MIN_VALUE gives hint to JDBC driver to stream results
query.setFetchSize(Integer.MIN_VALUE);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
// iterate over results
while (results.next()) {
    Object row = results.get();
    // process row then release reference
    // you may need to flush() as well
}
results.close();

Follow this blog post for more details.

Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113