-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: How to avoid OutOfMemoryError with large data sets??
PostPosted: Thu May 13, 2004 11:23 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
Hello

I'm a newbie to hibernate and testing for our purposes. We will have large datasets (several million entries), that are not presented all together, but page by page (maybe 500 entries).
I wrote a sample application, which simply prints the entries to the console (instead of a swing-gui).
I tried to use "ScrollableResults", but after about 700'000 entries I still get a "java.lang.OutOfMemoryError". I suppose it is, because it doesn't dispose old results... (?)

How can i process the results in a matter, that page by page is loaded from the database to the application?

I'm thankful for any type of advice!

thanks,
baer

system-info:
oracle 9 database
hibernate 2.1.3
if needed i'm happy to pass on mapping docs.

code:
Code:
private void printScrollableResult() {
    try {
      Configuration cfg = new Configuration();
      SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
      Session session = sessionFactory.openSession();
      String querySt = "select dyncdr_t from dyncdr_t " + " in class DyncdrT";
      Query q = session.createQuery(querySt);
      ScrollableResults cdrs = q.scroll();
      cdrs.beforeFirst();
      int i = 0;
      do {
        i = 0;
        while (i < PAGE_SIZE && cdrs.next()) {
          msg(new String("row = ") + new Integer(cdrs.getRowNumber()) + new String(": id=") + new String(cdrs.getString(0)));
          i++;
        }
        msg("------- end of page --------");

      }
      while (cdrs.scroll(PAGE_SIZE));     
    }
    catch (HibernateException he) {
      // TODO Auto-generated catch block
      he.printStackTrace();
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 5:06 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
I've found a solution.
Is this really the only way?
clear the session after every page?
Code:
     int pagecount=0;
     List result;
      Session session = sessionFactory.openSession();
      boolean loop = true;
      while (loop){   
        Query q = session.createQuery(querySt);
        msg("created query");
        q.setMaxResults(PAGE_SIZE);
        q.setFirstResult(pagecount*PAGE_SIZE);
        result = q.list();
        for (Iterator listIt = result.iterator(); listIt.hasNext();){
          Object o = listIt.next();
          DyncdrT dynT =  (DyncdrT) o;
          msg("page " + pagecount + " object is " + dynT.getCdrid());
        }       
        session.clear();
        pagecount++;
       
      }
      session.close();


I thank you for any pointer in this topic!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 5:09 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes. Hibernate (and ORM tools in general) are not useful for mass updates or deletes, use a Stored Procedure and don't load your full database into memory.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 6:55 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
christian wrote:
Yes. Hibernate (and ORM tools in general) are not useful for mass updates or deletes


actually, it is only a read operation...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 6:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
well, isn't the reason for paging in the first place not to have to load your whole dataset into memory? Why do you do it then, is this really a realistic testcase for your application?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:12 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
Thank you for your answers!

What I want to do:
We have large datasets (several million entries) which can be viewed through a gui.
Because no user really wants to go through this amount of data (and no system can handle it) I want to offer a function where only a part of the data is shown to the user (let's say 500 entries). The user shall be allowed to scroll through the results (as in google for example, where you can click on "next"...)
What I am testing now is what happens if the user went through 100 pages (which is really not very realistic...).
I just want to make sure that is not possible to create an outOfMemeoryException.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well you have to make sure the no longer needed data is evicted from the Session, thats the only way to do it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:15 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Query.setMaxResult()

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:16 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, wrong thread.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:20 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
michael wrote:
Well you have to make sure the no longer needed data is evicted from the Session, thats the only way to do it.

And is there an other way than calling Session.clear()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:21 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Session.evict() and Session.close().

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:36 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
One thing I don't understand:
It works fine when calling Session.clear().
However, when calling Session.evict(result) ( result = q.list();) or Session.evict(q) (Query q = session.createQuery(querySt);) it doesn't work (=outOfMemeoryError).

That means to me, that the results are not stored in the cache, but somewhere else in the session, where I don't have direct access. Is that correct?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:37 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Thats because you have to evict() persistent objects, not results or queries.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Read the api:
Quote:
evict

public void evict(Object object)
throws HibernateException

Remove this instance from the session cache. Changes to the instance will not be synchronized with the database.

Parameters:
object - a persistent instance
Throws:
HibernateException



Evict the items queried, not the result list or the query object.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:41 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
great - that should do it! (I missed the important words in the API:( - thanks for marking them in bold! )

THANK YOU!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.