3

I am getting the below error in my WebApp :

Handling an unexpected exception in webApp: - javax.servlet.error.status_code = 500 - javax.servlet.error.exception_type = class org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException - javax.servlet.error.message = Object of class [domain.entity.common.dataEntity] with identifier [110837262]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [domain.entity.common.dataEntity

I am not sure why this error occurs and it occurs in the below line.

SpecEntity specTemp = this.persistService.merge(specTemp);

Can someone please help here ..

1 Answer 1

11

In this scenario, the HibernateOptimisticLockingFailureException exception as it indicates happened when you tried to call merge on an entity (specTaskTemp) that was already updated in the mean time by another transaction.

If you look at SpecEntity class you must have defined @Version column. That will be used by the hibernate to check if the entity that you are merging is older than the one in the database.

Sample flow:

  1. Transaction T1 loads SpecEntity entity that has version column value as
  2. Transaction T2 loads the same SpecEntity with the version value 1.
  3. T2 makes some changes to this entity and updates the database and so the version value becomes 2.
  4. Subsequently T1 tries to update the entity.
  5. Hibernate checks if the version in the entity (i.e., 1) is same as that of value in the database (which is updated to 2).
  6. And when it finds the mismatch it throws HibernateOptimisticLockingFailureException.

Depending on your use case, you can either reload the updated entity from the database and show the updated data to the user so that user can decide what to do next.

Or if you want to continue overwriting the data in the database with data in the specTaskTemp entity, you can get the latest version of this entity from the database, copy its version value to the specTaskTemp entity and then invoke the merge call.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks or the reply.. I wanted to know things like .. why does it throw error on GenDataEntity as GenDataEntity extends SpecEntity .. Does this happen if there is merge or persist in the same session for SpecEntity .. Can we track the stale object by its ID- 110837262 in the database ..
@Teena Not sure if I can answer that without looking at your domain model and the flow of your app. Now since you know the crux of the problem and when this exception could happen, you can enable show_SQL property and see what exact sequence of events is causing the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.