为什么NHibernate的抛出" StaleObjectStateException"? [英] Why does NHibernate throw "StaleObjectStateException"?

查看:146
本文介绍了为什么NHibernate的抛出" StaleObjectStateException"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的项目和使用NHibernate 3.1

I am writing project and using NHibernate 3.1

SimpleTest的:

SimpleTest:

Forum forum = Session.CreateCriteria<Forum>().Add(Restrictions.Eq("UrlName", "reportabug")).UniqueResult<Forum>();
forum.TopicsCount++;
IForumRepository forumRepository = new ForumRepository(SessionFactory);
forumRepository.Update(forum);

public virtual void Update(TEntity entity)
{
    if (!session.Transaction.IsActive)
    {
        TResult result;
        using (var tx = session.BeginTransaction())
        {
            session.SaveOrUpdate(entity)
            tx.Commit();
        }
        return result;
    }
    session.SaveOrUpdate(entity)
}



最后更新抛出异常:

The last update throws exception:

StaleObjectStateException was unhandled by user code:
    Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

SQL查询:

UPDATE Forums
SET    Name = 'Forums Issues (not product support)' /* @p0 */,
       UrlName = 'reportabug' /* @p1 */,
       Description = 'Use this forum to report issues with the online forums application. When reporting an issue please include relevant details such as repro steps, error messages and browser version.' /* @p2 */,
       CategoryId = 'b2cc232c-0d5c-4f35-bb6f-29c67d7d40c2' /* @p3 */,
       TopicsCount = 1 /* @p4 */
WHERE  ForumId = '864046b7-ca57-48c4-8a81-082103223527' /* @p5 */

ForumId是正确的。
也许这就是并发?
有什么想法?

ForumId is correct. Maybe this is concurrency? Have any ideas?

推荐答案

StaleObjectStateException 是休眠确保数据的一致性阅读API的方式这里。 Hibernate的维护对象的版本,它更新,将抛出,如果在数据库和内存中的版本不匹配错误。了解更多关于乐观这里锁定机制

StaleObjectStateException is a way of hibernate ensuring data consistency read the API here. Hibernate maintains the version of objects it updates and will throw an error if the version in DB and in memory does not match. Read more about the optimistic locking mechanism here.

所以,调试与此信息的应用程序。我不熟悉的 C#语法,但我认为第二个保存应在其他的条件。

So, Debug the application with this information. I am not familiar with C# syntax but I think the second save should be in else condition.

这篇关于为什么NHibernate的抛出&QUOT; StaleObjectStateException&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆