EntityManager#lock throws throws OptimisticLockException using LockModeType.PESSIMISTIC_WRITE if entity doesn't exist in dataase
Description
None
Activity
Show:

rudolf.schoefl November 30, 2020 at 12:55 PMEdited

Former user October 15, 2019 at 6:18 PM
From Stephen Fikes:
“Aside from lock(), I think there is also an issue with EntityManager.find(...) which raises OptimisticLockException when using javax.persistence.LockModeType.PESSIMISTIC_WRITE (whether or not the entity has an optimistic version property). Per the JavaDoc [1] think it is supposed to be a PessimisticLockException (at least when not using a version property - not sure if it should differ when using a version property).”
Details
Details
Assignee
Unassigned
UnassignedReporter

Fix versions
Affects versions
Priority
Created October 8, 2019 at 10:23 PM
Updated September 29, 2021 at 9:09 PM
An easy way to reproduce this issue (using FlushMode.AUTO) is:
Surprisingly, when executing a query for the TestEntity before calling
entityManager.lock
, no exception will be thrown. The reason for that is, that, when callingSessionImpl.list
, SessionImpl will check if a flush is necessary and flush when needed.Could this code (check and autoflush) maybe be used in
entityManager.lock
too?