Interview questions
Question 202 of 254
FreeWhat is pessimistic locking, and when would you choose it over optimistic locking?
Advanced Spring Data JPA
Pessimistic locking acquires an actual database-level lock on a row at the moment it's read (via `LockModeType.PESSIMISTIC_READ` or `PESSIMISTIC_WRITE`), preventing other transactions from reading (for WRITE locks) or modifying the same row until the lock is released at transaction end — guaranteeing no conflicting concurrent update can happen at all, at the cost of reduced concurrency since competing transactions must wait. It's preferred when conflicts are expected to be frequent, or when the cost of a failed/retried optimistic update is unacceptable (e.g., a financial balance deduction where a lost-update retry is operationally awkward).
Advanced7 min
