Databases & Data Storage
Pessimistic Locking
Definition
Pessimistic locking is a locking strategy where a resource is locked from the time it is first accessed by a transaction until the transaction is finished. It assumes that concurrent transactions will likely conflict, so it locks resources preemptively.
Why It Matters
This strategy provides a high degree of safety, ensuring that no other transaction can modify the data while it is being worked on. It is useful in environments with high contention where conflicts are frequent.
Contextual Example
Before a user starts editing a document in a collaborative editor, the application could use pessimistic locking to lock the document. This prevents anyone else from editing it at the same time, avoiding conflicting changes.
Common Misunderstandings
- Pessimistic locking can severely limit concurrency if locks are held for a long time.
- It is contrasted with optimistic locking.