Pessimistic locking from JPA
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
lock rows from JPA when conflicts are likely.
Planned topics
@Lock(LockModeType.PESSIMISTIC_WRITE)on a repository query issuesSELECT ... FOR UPDATE.PESSIMISTIC_READmaps toFOR SHARE(verify dialect behavior before recording).- Set a lock timeout hint (
jakarta.persistence.lock.timeout) so threads do not wait forever. - Locks are held to the end of the transaction; keep it short.
Draft outline details
Demonstration
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select s from Stock s where s.productId = :id")
Optional<Stock> findForUpdate(Long id);Check your understanding
When is the lock released?
Show answer
When the transaction commits or rolls back.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.