Difference between flush and commit in Hibernate

Session flush is the process of synchronizing the underlying persistent store with persistable state held in memory. Flushing the session simply gets the data in the session synchronized with the database. If a persisted object in the Session has value change, it becomes dirty, and Session flush will update the database in the running transaction, but it may not commit those changes.

The behavior of the flushing mechanism is customizable, and a Session can set to different Flush Modes:

  • AWAYS – the Session is flushed before every query
  • AUTO – the Session is sometimes (when it is necessary) flushed before query execution in order to ensure that queries never return stale state. It is the default mode.
  • COMMIT – the Session is flushed when Transaction.commit() is called.
  • MANUAL – the Session is only ever flushed when Session.flush() is explicitly called by the application. This mode is very efficient for read only transactions.

Commit is a method of Hibernate Transaction interface, it will flush the associated Session and make the database commit (when Session is in FlushMode.MANUAL).

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">