Karl Gjertsen

Architect, Developer, Speaker, Bad Golfer,
Raygun brand ambassador and creator of Infiniforms

nHibernate: Flush vs Commit

Whilst working on a legacy project, I had to look at an issue with using nHibernate and ran into the Flush method. As I was new to nHibernate, I had to investigate what this did!

using (var transaction = session.BeginTransaction())

Calling Flush on the Session synchronises the data in the session with the data in the database.

Using Flush():


Calling Commit on the transaction calls flush on the session and then ends the unit of work, making the changes permanent.

Using Commit():


If you have called Flush(), you can still roll the data back. Once you call Commit(), the data cannot be rolled back.

It is worth remembering that nHibernate will flush the changes automatically for you in the following circumstances:

  • Before a query executes
  • When a transaction is committed
  • Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory
%d bloggers like this: