How NHibernate updates properties displayed as Version

Using Free NHibernate I have a property in a class displayed using Version

Version(x => x.Version);

When I save the object, the Version property is incremented in the database, as expected, but the value of the object property only changes occasionally.

using (var tx = session.BeginTransaction())
{
    session.Merge(item);
    tx.Commit();

    item.Version;  // Sometimes this is still 1, when I expect it to be 2.
}

The problem is that if it remains equal to 1, and I make more changes and save again, I get a StaleObjectStateException.

What is strange is that sometimes it works fine, and the value of item.Version increases correctly, but I can not understand the difference between the cases when it works and the cases when it is not.

, . - , NHibernates ?

[NHibernate version 2.1.2]

+3
3

item = session.Merge(item);
tx.Commit();

?

+1

ISession.Merge:

. , . . , . .

, item.

( , Merge . , , )

+2

, . , , .

You must TYPICALLY allow the session to clear itself when it is closed. However, in some cases, when you rely on database updates that happen using nhibernate, rather than the settings you make on the object itself, you may need to clear the session yourself after committing. In this case, keep in mind that when you clear a session, ANY objects that are dirty will be fixed. This may be undesirable, so make sure the area is very limited.

0
source

All Articles