-->
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.  [ 1 post ] 
Author Message
 Post subject: StaleObjectStateException in spite of identical versions
PostPosted: Fri Apr 16, 2010 9:09 am 
Newbie

Joined: Fri Apr 16, 2010 8:28 am
Posts: 1
Hello,
we are trying to use Hibernate with optimistic concurrency control. Our entities have the "@version" annotation so that hibernate can check if there has been an update from user B during the timespan from when user A has loaded the entity until user A commits his changes.
So let's assume that user A loads the entity (with the version 1, then user B commits his changes (setting the version in the db to 2), the user A tries to commit. In that case, Hibernate throws the infamous StaleObjectStateException, because there is a conflict.

Now let's now assume that user A wants to accept the changes of user B. So he does something like:

try
{
Session session = transactionManager.getSession();
org.hibernate.Transaction tx = session.beginTransaction();
session.persist(entity);
tx.commit();

}catch(StaleObjectStateException ex){

String entityName = ex.getEntityName();
Serializable identifier = ex.getIdentifier();
tx.rollback();

BusinessObject modifiedObject = entity;
BusinessObject dataBaseState = (BusinessObject) session.get(entityName, identifier);
//System.out.println(modifiedObject.getVersion()); //displays 1
//System.out.println(dataBaseState.getVersion()); //displays 2

session.refresh(modifiedObject);
//System.err.println(modifiedObject.getVersion()); //displays 2

//Try to commit again
Session session = transactionManager.getSession();
org.hibernate.Transaction tx = session.beginTransaction();
session.persist();
tx.commit(modifiedObject); //Throws an StaleObjectStateException again, in spite of having identical version numbers!!!!!
}

Without any other user interfering, this again throws a StaleObjectStateException, although after session.refresh(entity), the entity is up-to-date to the database (in particular, the version is set to 2).

Why is it that Hibernate sees a conflict although the entity is up-to-date to the database?

Many thanks!


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

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.