Hibernate version : 3.2.6.ga
Spring(transactions) : 3.1.1
I am facing this in a batch process where I don't have to notify any end user ,just need to reload the entities involved & redo the work.
However, even after catching the StaleObjectStateException & reloading the entity, I am still running into StaleObjectStateException..why is that?
More details on how I am reproducing this is after the code...
Code:
@RequestMapping("testConcurrent/{entityId}/{version}")
@GET
@Transactional
public void testConcurrent(@PathVariable("entityId") Integer entityId,
@PathVariable("version") Integer version){
try{
updateData(entityId, version);
sessionFactory.getCurrentSession().flush();
}
catch(StaleObjectStateException|OptimisticLockException e){
LOG.info("retrying due to concurrency error");
updateData(entityId, version);
}
catch(Exception e){
LOG.info("some other error");
}
}
public void updateData(Integer entityId, Integer version) {
Entity entity = entityDAO.getEntityById(entityId);
entity.setUpdated(DateUtils.getCurrentTime());
entity.setVersion(version.longValue());
entityDAO.createOrUpdateEntity(crp);
}
How i reproduce concurrency:
1)I call testConcurrent for a given object with some version, i stop the debug flow at the last line in updateData method.
2)Then I call testConcurrent again for the same object with some other version, and finish the flow. This reflects in the DB instantaneously as i have a flush() after this method returns.
3)Now when i continue the first thread(which was blocked in step 1), it throws this StaleObjectStateException as expected & it gets caught, calls the updateData method again , but now when i fetch the object from DB(entityDAO.getEntityById); it throws the same StaleObjectState exception again. Why should it when i am reloading the latest object from database?
This is the exception stacktrace by the way , Could not synchronize database state with session org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):