无法将数据库状态与session-Nhibernate和Rebus并发问题同步

时间:2016-07-29 09:17:42

标签: nhibernate rebus

将Rebus与Nhibernate一起使用时,将用户详细信息存储在表中会出现错误,如

NHibernate.Event.Default.AbstractFlushingEventListener

NHibernate.AdoNet.TooManyRowsAffectedException: Unexpected row count: 5; expected: 1
   at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, 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)

这可能由于所有用户并发而发生,因为如果我使用调试器,那么它会在足够的时间过后意外工作。

在此代码中获取异常

  public void Insert(ISagaData sagaData, string[] sagaDataPropertyPathsToIndex)
        {
            try
            {
                var nSagaData = new NDbSagaData
                {
                    Id = sagaData.Id,
                    Revision = sagaData.Revision++,
                    Data = JsonConvert.SerializeObject(sagaData, Formatting.Indented, Settings)
                };
                PersistenceManager.Save(nSagaData);
            }
            catch (Exception ex)
            {
                _log.Error("Insert Exception " + ex.Message);
                System.IO.File.AppendAllText("C:\\TestFolder\\WriteText.txt", "Insert fromSource - " + ex.Message);
                throw new OptimisticLockingException(sagaData, ex);
            }
        }

它说,' 意外的行数:3;预期:1 '

原因是什么?

1 个答案:

答案 0 :(得分:0)

看起来你已经实现了一个基于NHibernate的saga persister,它似乎已经检测到竞争条件,我猜测它会导致回滚事务。

我想问题是为什么发生了竞争条件。当NHibernate的乐观并发检查失败时会发生“意外行计数”异常,但在这种情况下,我会预期会有一条消息“意外行数0;预期为1”。

如果我是你,我会使用Rebus的内置SQL Server saga persister,因为看起来你无论如何都要将saga数据序列化为一个列。