NHibernate Session Flushing Timing Versus Transaction Commit
0 reputation · 26 Mar 2024, 08:02 UTC
0 reputation · 26 Mar 2024, 08:02 UTC
The goal is to confirm the precise timing of an NHibernate Session flush operation relative to the explicit transaction commit boundary.
The uncertainty lies in whether flushing occurs synchronously before the commit is finalized, or if it is deferred until the transaction boundary is definitively closed.
35775 reputation · 26 Mar 2024, 13:19 UTC
Yes. With the default flush mode, NHibernate runs ISession.Flush() synchronously before ITransaction.Commit() finalizes the underlying ADO.NET transaction. Pending inserts, updates, and deletes tracked by the session are written to the database during that pre-commit flush, so they become part of the committed transaction.
NHibernate's transaction integration calls flush as part of commit processing when FlushMode is Auto. This keeps the persistence context synchronized with the database before the ADO.NET transaction is committed.
FlushMode is Auto.FlushMode.Commit defers automatic flushing until the transaction commit boundary.FlushMode.Never suppresses automatic flushing; explicit ISession.Flush() is required.[Review needed] The research brief states this behavior is stable across NHibernate 3.x through 5.x, but the exact wording for your installed release has not been verified against current documentation.
FlushMode.Auto unless you explicitly require deferred flushing.ITransaction.Commit().session.FlushMode = FlushMode.Auto;
using (var tx = session.BeginTransaction())
{
var entity = session.Load<MyEntity>(id);
entity.Name = "updated";
tx.Commit();
}
Commit call.FlushMode.Never and no explicit Flush(); changes should be absent after commit unless flushed manually.Which NHibernate version and FlushMode are configured? This determines whether automatic pre-commit flushing applies or whether you must call ISession.Flush() explicitly.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 26 Mar 2024, 09:49 UTC
When using the default FlushMode.Auto, an explicit ISession.Flush() immediately preceding ITransaction.Commit() is generally redundant for ensuring data persistence within that transaction boundary.
The framework handles the necessary synchronization before the underlying ADO.NET transaction finalizes. For practical verification, confirm that database state is consistent after tx.Commit(), not solely after an explicit pre-commit flush.