| The problem here is that an EntityVerifyVersionProcess is registered with the EntityEntry associated with the entity at the time that the entity is locked. Later, when the entity is refreshed, that EntityEntry is replaced by a new one in the PersistenceContext. This makes the EntityEntry registered with the EntityVerifyVersionProcess stale. After the updated entity is pushed to the database by EntityUpdateAction#execute, which calls EntityEntry#postUpdate to update the version in the entity and the current EntityEntry. Later, when EntityVerifyVersionProcess#doBeforeTransactionCompletion executes using the stale EntityEntry, OptimisticLockException is thrown because the version in the stale EntityEntry does not match the current version. Workarounds:
- lock the entity when it is refreshed; i.e., entityManager.refresh(entity, LockModeType.READ);
- refresh the entity first, then lock it.
|