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.