Difference in PESSIMISTIC_WRITE and UPGRADE lock modes in Hibernate
Issue
- A legacy application is using LockMode.UPGRADE with Session.get(...) to apply a pessimistic lock to an entity row.
UPGRADE
is marked deprecated so the lock mode was changed to PESSIMISTIC_WRITE during migration from an older Hibernate release.- In cases where the entity row does not already exist in the database (e.g.
persist(...)
has been called but the pending insert has not yet been flushed to the database), use ofPESSIMISTIC_WRITE
fails with an exception. If the entity exists, a lock is obtained as expected.
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 7
- 6
- Hibernate
- 5
- 4
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.