, ,

flush(): Forces the session to flush. It is used to synchronize session data with database.

When you interact with Hibernate, you do so via a Hibernate session. Hibernate sessions are flushed to the database in three situations:

  • When you commit a (Hibernate) transaction.
  • Before you run a query.
  • When you call session.flush().


In the Hibernate Manual you can see this example

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    if ( i % 20 == 0 ) { //20, same as the JDBC batch size
        //flush a batch of inserts and release memory:


Without the call to the flush method, your first-level cache would throw an OutOfMemoryException.


When you call session.flush(), the statements are executed in database but it will not committed.

If you dont call session.flush() and if you call session.commit() , internally commit() method executes the statement and commits.

So commit()= flush+commit.

So seesion.flush() just executes the statements in database (but not commits) and statements are NOT IN MEMORY anymore. It just forces the session to flush.