Reputation: 113
I am novice in hibernate. i am having a doubt regarding transactions and flushing..
whenever we want to persist an object we open a session and begin a transaction. after persisting which one of these comes first and the other follows ......
1)session.getTransaction.commit(); 2)session.flush()
According to what i have read commit() commits values to the database. flush() syncs objects state to the database.
what is the difference between these two.
Upvotes: 5
Views: 8457
Reputation: 90457
session.flush()
will do the flushing process which is about doing dirty check for all persistent objects managed by the hibernate session.If an object is considered to be dirty (i.e any values of the object stored in that hibernate session are different from the corresponding record in the database ) , hibernate will issue UPDATE SQLs to the DB immediately in order to synchronize these differences to make the object stored in the hibernate session has the same values with the corresponding database record.
However ,just issuing the UPDATE SQL does not mean that the modified data is actually saved to the DB ,you have to COMMIT the transaction in order to confirm saving the modified data to the DB actually .It also means that you can ROLLBACK the changes made by the UPDATE SQL if any errors are found after issuing the UPDATE SQL but before committing the transaction.
The flushing behavior of a hibernate session is determined by the FlushMode
parameters which can be configured by session.setFlushMode() . The default value is FlushMode.AUTO
such that session.flush()
will occurs automatically before committing an transaction and execution of the query.
So , when session.getTransaction.commit()
is called in the default FlushMode
, session.flush()
will be executed implicitly before execution of session.getTransaction.commit()
.
Upvotes: 9