As stated in flush docs java
Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.
Here is my understanding of above statement
So if somebody is doing insert/update and then flush, that extra inserted rows will be lying in java memory only. But other way around i.e Db data will only be synchronized with persistable state held in memory on commit only.
Now lets go by above understanding
I came across HIbernate commit() and flush() where accepeted answer tells session.flush helps in releasing memory in some cases and hence avoid OutOfMemoryException.
when i do below , line1 (session.flush()) will execute the insert query for 20 customers on customer table which release the memory for 20 customer objects in list but on the other hand creates 20 customer data rows under customer table which are still in java memory(it will go to database only on commit at line2). So i am not sure how session.flush helps here in releasing the memory ?
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush(); //line1
session.clear();
}
}
tx.commit();// line2
session.close();