Disable Version Increment on Session.SaveOrUpdate()

450 views
Skip to first unread message

j gwood

unread,
Jan 6, 2009, 9:25:22 AM1/6/09
to nhusers
Is there any way to have the Version property NOT incremented on
Session.SaveOrUpdate() or Session.Update() when using optimistic-
lock="false"? I am using setting optimisitic-lock to false in my
mapping file; however, NHibernate seems to be ignoring the optimistic-
lock attribute setting. Is there some other property that needs to be
set on my component or list?

We are encapsulating NHibernate in our Persistence layer and using
Spring.NET to help manage transactions, so we are not exposing
ISession. The optimistic-lock attribute works when you don't call
Session.SaveOrUpdate and when you simply commit a transaction:

s = OpenSession();
t = s.BeginTransaction();
gavin = (Person) s.CreateCriteria(typeof(Person)).UniqueResult
();
new Task("Document", gavin);
t.Commit();
s.Close();

My current mapping file as the following attributes set:

<class name="Parent" table="Parent" schema="Simple" proxy="IParent"
optimistic-lock="version">
<id name="Id" column="Id" type="Guid">
<generator class="assigned"/>
</id>
<version name="Version" column="`Version`" type="Int32" unsaved-
value="0"/>
<property name="Name" column="`Name`" type="string" length="50"
optimistic-lock="false" />
<list name="ChildrenInjector" inverse="true" generic="true"
cascade="all-delete-orphan" batch-size="10" optimistic-lock="false">
<key column="ParentId"/>
<index column="SiblingIndex" type="Int32"/>
<one-to-many class="Child"/>
</list>
</class>

Any suggestions are greatly appreciated,

Jean

Fabio Maulo

unread,
Jan 6, 2009, 9:53:49 AM1/6/09
to nhu...@googlegroups.com
The version is the version. The optimistic-lock strategy is the optimistic-lock strategy.
If you are working with Version, each operation with a persistence instance mean change its state that mean change its version.

The optimistic-lock have place when an instance is flushed to DB. If you don't set the FlushMode explicit its value is FlushMode.Auto that mean, at least, that you are flushing the nh-session (that is the UoW) at transaction-commit.

2009/1/6 j gwood <jean.gr...@gmail.com>



--
Fabio Maulo

j gwood

unread,
Jan 6, 2009, 10:39:34 AM1/6/09
to nhusers
Thank you for the explanation on version versus optimistic-lock. The
following text from the NHibernate In Action book led me to believe
that you could disable the version increment using the optimistic-lock
attribute (page 129 of Kuate_NHibernate_MEAP_0804.pdf):

"It is possible to disable the increment of the version when a
specific property is dirty. To do so, you must add optimistic-
lock="false" to this property’s mapping. This feature is available
for properties, components and collections (the owning entity's
version is not incremented). "

The book does not mention the difference between session.save() and
flushing the session with transaction.commit().

My company requires the functionality to save an object without having
the version incremented when certain properties are modified.

Jean

On Jan 6, 9:53 am, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> The version is the version. The optimistic-lock strategy is
> the optimistic-lock strategy.If you are working with Version, each operation
> with a persistence instance mean change its state that mean change its
> version.
>
> The optimistic-lock have place when an instance is flushed to DB. If you
> don't set the FlushMode explicit its value is FlushMode.Auto that mean, at
> least, that you are flushing the nh-session (that is the UoW) at
> transaction-commit.
>
> 2009/1/6 j gwood <jean.greenw...@gmail.com>
> Fabio Maulo- Hide quoted text -
>
> - Show quoted text -

Fabio Maulo

unread,
Jan 6, 2009, 10:43:45 AM1/6/09
to nhu...@googlegroups.com
NHiA is based on NH1.2 (very old version at this point) and is strange that I don't know that feature.
I'll review the H3.3.0 code to check if that is a real feature supported by Hibernate.

2009/1/6 j gwood <jean.gr...@gmail.com>



--
Fabio Maulo

j gwood

unread,
Jan 6, 2009, 11:02:25 AM1/6/09
to nhusers
Hibernate documentation states the following for a property (5.1.11):

"(10) optimistic-lock (optional - defaults to true): Specifies that
updates to this property do or do not require acquisition of the
optimistic lock. In other words, determines if a version increment
should occur when this property is dirty. "

However, none of the doucmentation that I could find discusses the
difference between flushing the session and explicitly calling Save
(). Even if I save an object without making any changes to it, the
version property gets incremeted.

Jean

On Jan 6, 10:43 am, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> NHiA is based on NH1.2 (very old version at this point) and is strange that
> I don't know that feature.I'll review the H3.3.0 code to check if that is a
> real feature supported by Hibernate.
>
> 2009/1/6 j gwood <jean.greenw...@gmail.com>

Fabio Maulo

unread,
Jan 6, 2009, 11:20:00 AM1/6/09
to nhu...@googlegroups.com
I'm sure that there is something saying the difference between a session.Save, session.Update, session.Delete and session.Flush...
If I well remember was one of the first thing I learn in NH.
BTW some concepts are here



--
Fabio Maulo

Fabio Maulo

unread,
Jan 6, 2009, 11:22:22 AM1/6/09
to nhu...@googlegroups.com

j gwood

unread,
Jan 16, 2009, 11:16:04 AM1/16/09
to nhusers
After stepping through the NHibernate code, I figured out the problem
and found a temporary solution. As I stated earlier, we are using
Spring.NET along with NHibernate inside of our persistence layer. For
some reason, our entity objects are not getting stored in the cashed
database snapshot (I need to look into this in more detail), and it is
this cashed database snapshot that NHibernate checks to determine
whether or not properties are dirty and whether or not the property
being dirty should cause a version increment.

For any mapping file that has a property with the optimistic-lock
attribute set to false, I added the select-before-update="true"
attribute to the class. This forces NHibernate to get the entity
state from the database when checking for dirty properties. Now the
version property is being correctly updated (or not updated) as I
would expect (based on the optimistic-lock="false" attribute).

I know that we may hit some performance issues with this temporary
solution, which is why it is only temporary. Once I figure out why
our objects are not getting cached in the database snapshot (need to
investigate how Spring.NET is managing the Session), I will update
this post.

Fabio, thanks for your helpful links!

On Jan 6, 11:22 am, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> http://nhforge.org/doc/nh/en/index.html#manipulatingdata-flushinghttp://nhforge.org/doc/nh/en/index.html#transactions-optimistic
>
> 2009/1/6 Fabio Maulo <fabioma...@gmail.com>
>
>
>
>
>
> > I'm sure that there is something saying the difference between a
> > session.Save, session.Update, session.Delete and session.Flush...If I well
> > remember was one of the first thing I learn in NH.
> > BTW some concepts are here
> >http://fabiomaulo.blogspot.com/2008/12/identity-never-ending-story.html
>
> > 2009/1/6 j gwood <jean.greenw...@gmail.com>
Reply all
Reply to author
Forward
0 new messages