Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 query:

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 is correct. Maybe this is concurrency? Have any ideas?

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.

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.

Thats correct, I totally missed that :P +1. @Kovpaev - Remove the last session.SaveOrUpdate(entity), there is no need for it. – Ash Burlaczenko Nov 2, 2011 at 8:25 @Ash Burlaczenko, call at the end of the method is needed if the method Update() is invoked in the context of outer transaction. If not, it is created in the block "if". – Kovpaev Alexey Nov 2, 2011 at 19:24

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.