1

Came across this example at HIbernate commit() and flush()

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();
        session.clear();
    }
}

tx.commit();
session.close();

My understanding was hibernate synchronizes the DB session with hibernate when we do session.save(customer) or session.update(....) ? Does it sunchromized only at the time of commit/flush/refresh not at the time of update/save ?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
emilly
  • 10,060
  • 33
  • 97
  • 172

2 Answers2

1

The Hibernate Session and the JPA EntityManager (a.k.a Persistence Context) act as a write-behind cache so entity state transitions are staged and propagated to the DB during flush.

By default, the commit call triggers a flush and that's when INSERT, UPDATE, and DELETE statements are executed.

The benefits of the write-behind cache are the followings:

  • it's much easier to apply automatic JDBC batch updates
  • if there is no prior SELECT statement, the connection acquisition can be delayed until the flush, therefore reducing the database connection lease time.

For more details about how Hibernate works, check out this tutorial which features over 100 articles about JPA, Hibernate, and the most common RDBMS.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • In my test case I don't see any insert/update query is being fired when I do `session.save(customer) or session.update(....)`. It happens only either at commit or flush happens. so your statement `and that's when INSERT, UPDATE, and DELETE statements are executed.` does not seem to be correct here ? – emilly Apr 20 '17 at 07:27
  • You are actually confirming my statement. Read it carefully – Vlad Mihalcea Apr 20 '17 at 08:47
0

The lifecycle of a Hibernate Session is bounded by the beginning and end of a logical transaction. Session offers create, read and delete operations for instances of mapped entity classes. Save/update/delete are transactional operations.So to execute those operations, there is need to begin transaction with

session.beginTransaction();

and then only we can execute save/update/delete. Hibernate provides good quality of transaction rollback mechanism. Hibernate makes changes to database only if transaction is committed through

session.getTransaction().commit();

Till commit is executed save/update/delete will be with transaction instance, and once commit method is invoked hibernate interacts with database to apply changes.

If save/update/delete fails, rollback process will be executed, leaving database unchanged.

for more information about hibernate transaction study https://www.javatpoint.com/hibernate-transaction-management-example

So the hibernate synchronizes the DB session with hibernate session, through commit/flush/refresh

vsbehere
  • 666
  • 1
  • 7
  • 23