Difference in PESSIMISTIC_WRITE and UPGRADE lock modes in Hibernate

Solution Verified - Updated -

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 of PESSIMISTIC_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.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content