-->
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.  [ 3 posts ] 
Author Message
 Post subject: flush in Hibernate - What does it mean?
PostPosted: Thu Sep 07, 2006 4:49 am 
Newbie

Joined: Thu Sep 07, 2006 4:35 am
Posts: 9
Hi,
We are using Hibernate-3.x in our application. We have a code where we create large number of hibernate objects(mapped to tables) as part of a single transaction. We create the Session during this process. Here's a pseudo code:
Code:
myMethod() {

for (i=0;i<800000;i++) {

  Session sess = getHibernateSession();

  MyHibernateObject obj = createHibernateObject();

  sess.save(obj);
  count++;
  if (count >= 50) {
     sess.flush();
    sess.clear();
  }


}
}


In the above code, the myMethod is called as part of a transaction on a SessionBean. The loop in the code will be iterated around 60K to 80K times. As can be seen in the code, the hibernate objects are created and saved in this loop. Since this is part of the transaction, the actual save to database never happens until the transaction is committed. The objects to be saved are maintained in memory by the Hibernate Session.
So to avoid OutOfMemory issues, we introduced the logic of flushing and clearing the session for every 50 objects.

As per the documentation of the flush API:
Quote:
Force the Session to flush. Must be called at the end of a unit of work, before commiting the transaction and closing the session (Transaction.commit() calls this method). Flushing is the process of synchronising the underlying persistent store with persistable state held in memory.


Does this mean that the save/update queries will be issued by Hibernate when we invoke the flush method? Or is it the other way around, is Hibernate going to repopulate the contents of the Session using the information from the database? The reason i ask this is because if its the latter then we are going to lose the objects that we had maintained in the Session for being saved later.

Any help in understanding this, is appreciated.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 6:33 am 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
AFAIK the flush() will just "commit" any pending changes in hibernate session to the underlying persistent store. Not the other way around. Hibernate does not populate its session with data, unless you ask it to do so (session.get(), session.load(), ...).

But in my application I never explicitly call the flush() method. And I guess it is better to let hibernate handle it when needed. For example I use the "FLUSH_ON_COMMIT" policy, and use a transaction.commit() whenever I want it.

Your pseudo code will then became:
Code:
session = openConnection();
Transaction tx = session.beginTransaction();
try {
for (50 objects) {
session.save(object);
}
tx.commit();
} catch (Exception e) {
// tx.rollback()
}
session.clear();


A this way you could also call "tx.rollback()" when needed (to handle error recovery for instance)

--
Hugo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 6:44 am 
Newbie

Joined: Thu Sep 07, 2006 4:35 am
Posts: 9
Quote:
and use a transaction.commit() whenever I want it.


The code that i mentioned is in a SessionBean and it is part of Container Manager Transaction(CMT), so we are not allowed to invoke the tx.commit() or tx.rollback().

Quote:
AFAIK the flush() will just "commit" any pending changes in hibernate session to the underlying persistent store. Not the other way around. Hibernate does not populate its session with data, unless you ask it to do so (session.get(), session.load(), ...).


Makes sense. Thanks, Hugo.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.