Pessimistic Locking
PronunciationPES-ih-MIS-tik LOK-ing
Definition
Pessimistic locking locks a row as soon as you read it for updating (SELECT ... FOR UPDATE), so other transactions must wait until you finish. It assumes conflicts are likely.
Where you hear it
In booking, balances and counters where two requests must not act on the same row, and Django's select_for_update().
Examples
Lock the account row with
select_for_update()before changing the balance.Keep the transaction short so the lock isn't held long.
The bank transfer locks both account rows first, so two transfers cannot overlap.
Common mistake
Locking rows in different orders in different code paths. That is the classic recipe for a deadlock.
Don't confuse with
Optimistic locking, which takes no lock and detects conflicts at save time.
Say it at work
- In a meeting or chat
Use select_for_update inside atomic.
- In an email or ticket
Someone is holding the lock.