OptimisticLockException after locking, refreshing, and updating an entity

Description

The following steps involving update of an entity with a Version property in a transaction result in an OptimisticLockException during commit.

  1. Employee e = entityManager.find(Employee.class, THE_KEY_HERE);

  2. entityManager.lock(e, LockModeType.READ);

  3. entityManager.refresh(e);

  4. Update some property of the entity (and merge)

  5. Commit

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... javax.persistence.OptimisticLockException: Newer version [2] of entity [[support.hibernate.entity.Employee#THE_KEY_HERE]] found in database at org.hibernate.internal.ExceptionConverterImpl.wrapLockException(ExceptionConverterImpl.java:247) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:103) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:60) ... 29 more Caused by: org.hibernate.OptimisticLockException: Newer version [2] of entity [[support.hibernate.entity.Employee#THE_KEY_HERE]] found in database at org.hibernate.action.internal.EntityVerifyVersionProcess.doBeforeTransactionCompletion(EntityVerifyVersionProcess.java:51) at org.hibernate.engine.spi.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:954) at org.hibernate.engine.spi.ActionQueue.beforeTransactionCompletion(ActionQueue.java:525) at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2487) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:39) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271) at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104) ...

 

Environment

None

Status

Assignee

Gail Badner

Reporter

Stephen Fikes

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.4.3
5.3.10

Priority

Major