-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Could not synchronize database state with session. Stale
PostPosted: Thu Sep 03, 2009 3:21 pm 
Newbie

Joined: Thu Sep 03, 2009 3:04 pm
Posts: 2
I have the following mapping

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Domain.User, Domain, Culture=neutral, PublicKeyToken=0113ab8eb077993" table="tblUser">
      <cache usage="read-write"/>
            <id name="ID" type="Int32" unsaved-value="0">
         <column name="UserNID" sql-type="int" not-null="true" unique="true" index="PK_UserNID"/>
         <generator class="native" />
      </id>
      <timestamp column="LastModifiedDTM" name="LastModifiedDtm" unsaved-value="undefined"/>
      <property name="UserStatusID" type="Int32">
         <column name="UserStatusID" sql-type="int" not-null="true"/>
      </property>
      <property name="Permissions" type="Int32">
         <column name="Permissions" sql-type="int" not-null="true"/>
      </property>
      <property name="LastName" type="String">
         <column name="LastName" length="50" sql-type="varchar" not-null="true"/>
      </property>
      <property name="FirstName" type="String">
         <column name="FirstName" length="50" sql-type="varchar" not-null="true"/>
      </property>
      <property name="MiddleInitial" type="String">
         <column name="MiddleInitial" length="1" sql-type="char" not-null="true"/>
      </property>
                ...........
                ...........
               <many-to-one name="Office" class="Domain.Office, Domain, Culture=neutral, PublicKeyToken=0113ab8eb077993">
         <column name="OfficeNID" sql-type="int" not-null="true"/>
      </many-to-one>
               <many-to-one name="Role" class="Domain.Role, Domain, Culture=neutral, PublicKeyToken=0113ab8eb077993">
         <column name="RoleID" sql-type="int" not-null="true"/>
      </many-to-one>
                <one-to-one name="UserPlanRole" class="Domain.UserPlanRole, Domain, Culture=neutral, PublicKeyToken=0113ab8eb077993" />
      <bag name="UserContactInfo" inverse="true" lazy="false">
         <key column="UserNID"/>
         <one-to-many class="Domain.UserContactInfo, Domain, Culture=neutral, PublicKeyToken=0113ab8eb077993"/>
      </bag>
   </class>
</hibernate-mapping>


When I do update I get following exception

Code:
2009-09-03 14:53:19,982 ERROR Could not synchronize database state with session
NHibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Domain.User#6700]
   at NHibernate.Persister.Entity.AbstractEntityPersister.Check(Int32 rows, Object id, Int32 tableNumber, IExpectation expectation, IDbCommand statement)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
   at NHibernate.Action.EntityUpdateAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)


Ironically this update works in my Sql 2005 server but not in Sql 2008 or Sql 2000 server(at least for the few cases I tried in Sql 2k5; different data but same schema though).

Also I see that my row is getting updated in Sql 2008. After the db update its failing with above exception.

Please note that my timestamp LastModifiedDTM column is DateTime in Sql Server.

Any help on the error would be appreciated

Thanks


Top
 Profile  
 
 Post subject: Re: Could not synchronize database state with session. Stale
PostPosted: Wed Sep 09, 2009 11:15 am 
Newbie

Joined: Thu Sep 03, 2009 3:04 pm
Posts: 2
I found the exact problem. The error was misleading. The NOCOUNT was ON at the server level. Update does ExecuteNonQuery and for optimistic locking it relies on the NOCOUNT being OFF. Since NOCOUNT was ON my cmd returns -1 for the number of rows changed.

Unfortunately I cannot change at the server level so created a new connection provider which sets NOCOUNT OFF for that connection.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.