Problems with NHibernate FlushMode Never

83 views
Skip to first unread message

pvginkel

unread,
Sep 12, 2013, 6:22:43 AM9/12/13
to nhu...@googlegroups.com

We're building a large application with NHibernate as ORM layer. We've tried to apply as many best practices as possible, among which setting FlushMode to Never. However, this is giving us pain, for example the following scenario:

There is a table with an end date column. From this table, we delete the last (by end date) record:

  • The record is deleted;
  • After the delete, we do a (repository) query for the last record (by end date);
  • This last record is updated because it's the new active record.

This is a very simple scenario, of which many exist. The problem here is that when we do the query, we get the deleted record back, which of course is not correct. This roughly means that we cannot do queries in business logic that may touch the entity being inserted or deleted, because its resp. not there yet or still there.

How can I work with this scenario? Are there ways to work around this without reverting the FlushModesetting or should I just give up on the FlushMode setting all together?

(this is a cross-post from http://stackoverflow.com/questions/18761613/problems-with-nhibernate-flushmode-never)

Oskar Berggren

unread,
Sep 13, 2013, 3:56:37 AM9/13/13
to nhu...@googlegroups.com
I would say the NHibernate tries very hard to show a consistent state, so that your business logic doesn't have to be aware of such nuances. Especially important if one use e.g. a domain events system or other method whereby domain logic may be composed from several modules into a single flow.

I normally stick with FlushMode.Auto as much as possibly. Other flush modes can be useful in smaller, very controlled, parts of the code, if it's required to gain the required performance.

So either stick with FlushMode.Auto, and you business logic doesn't need to care. Or use another FlushMode, and you must make your business logic have the knowledge to compensate as needed.

Where is it claimed that FlushMode.Never is best practice? I would like to see their arguments.

/Oskar




2013/9/12 pvginkel <pvgi...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Pieter van Ginkel

unread,
Sep 20, 2013, 5:55:41 AM9/20/13
to NHibernate Users Group
The primary reason I've applied this FlushMode is because I've incorporated the CpBT model. There are a number of places where it is mentioned that this works with the FlushMode.Never setting, e.g. http://fabiomaulo.blogspot.nl/2008/12/identity-never-ending-story.html. And, of course, the uNhAddIns applies this in its implementation in https://code.google.com/p/unhaddins/source/browse/uNhAddIns/uNhAddIns/SessionEasier/Conversations/NhConversation.cs with:

private static void EnsureFlushMode(ISession session)
{
if (session.FlushMode != FlushMode.Never)
{
log.Debug("Disabling automatic flushing of the Session");
session.FlushMode = FlushMode.Never;
}
}


--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/aNQm94ffkrk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages