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:
ISession.Get(typeof(T), id)
myCar.Color="Green"
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?
Flushing synchronizes the persistent store with in-memory changes but not vice-versa. Note that for all NHibernate ADO.NET connections/transactions, the transaction isolation level for that connection applies to all operations executed by NHibernate!
The NHibernate session encapsulates a unit of work as specified by the unit of work pattern.
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
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With