22

I am kind of confused on how Flush ( and NHibernate.ISession) in NHibernate works.

From my code, it seems that when I saved an object by using ISession.Save(entity), the object can be saved directly to the database.

However, when I update and object using ISession.SaveOrUpdate(entity) or ISession.Update(entity), the object in the database is not updated--- I need to call ISession.Flush in order to update it.

The procedure on how I update the object is as follows:

  1. Obtain the object from the database by using ISession.Get(typeof(T), id)
  2. Change the object property, for example, myCar.Color="Green"
  3. Commit it back to the database by using ISession.Update(myCar)

The myCar is not updated to database. However, if I call ISession.Flush afterwards, then it is updated.

When to use Flush, and when not to use it?

Graviton
  • 81,782
  • 146
  • 424
  • 602
  • possible duplicate of [NHibernate ISession Flush: Where and when to use it, and why?](http://stackoverflow.com/questions/43320/nhibernate-isession-flush-where-and-when-to-use-it-and-why) – sleske May 10 '14 at 13:12

2 Answers2

30

In many cases you don't have to care when NHibernate flushes.

You only need to call flush if you created your own connection because NHibernate doesn't know when you commit on it.

What is really important for you is the transaction. During the transaction, you are isolated from other transactions, this means, you always see your changes when you read form the database, and you don't see others changes (unless they are committed). So you don't have to care when NHibernate updates data in the database unless it is committed. It is not visible to anyone anyway.

NHibernate flushes if

  • you call commit
  • before queries to ensure that you filter by the actual state in memory
  • when you call flush

Example:

using (session = factory.CreateSession())
using (session.BeginTransaction())
{
  var entity = session.Get<Entity>(2);
  entity.Name = "new name";

  // there is no update. NHibernate flushes the changes.

  session.Transaction.Commit();
  session.Close();
}

The entity is updated on commit. NHibernate sees that your session is dirty and flushes the changes to the database. You need update and save only if you made the changes outside of the session. (This means with a detached entity, that is an entity that is not known by the session).


Notes on performance: Flush not only performs the required SQL statements to update the database. It also searches for changes in memory. Since there is no dirty flag on POCOs, it needs to compare every property of every object in the session to its first level cache. This may become a performance problem when it is done too often. There are a couple of things you can do to avoid performance problems:

  • Do not flush in loops
  • Avoid serialized objects (serialization is required to check for changes)
  • Use read-only entities when appropriate
  • Set mutable = false when appropriate
  • When using custom types in properties, implement efficient Equals methods
  • Carefully disable auto-flush when you are sure that you know what you are doing.
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • Thanks, but when I update an object in the database, does this mean that I create my own transaction? Or else why do I need to call flush to update the database? – Graviton Apr 23 '09 at 09:31
  • 1
    Had to fix it: you need to flush when you created your own connection. NHibernate creates a connection for you if you call CreateSession, but you can provide you own ADO.NET connection. usually you don't need this. You don't need to call flush if you don't do this. – Stefan Steinegger Apr 23 '09 at 10:04
6

NHibernate will only perform SQL statements when it is necessary. It will postpone the execution of SQL statements as long as possible.

For instance, when you save an entity which has an assigned id, it will likely postpone the executin of the INSERT statement. However, when you insert an entity which has an auto-increment id for instance, then NHibernate needs to INSERT the entity directly, since it has to know the id that will be assigned to this entity.

When you explicitly call flush, then NHibernate will execute the SQL statements that are necessary for objects that have been changed / created / deleted in that session.

Flush

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154