Difficulty: Medium
Correct Answer: The lock is obtained only when the transaction is ready to write or commit changes.
Explanation:
Introduction / Context:Optimistic locking is a concurrency control strategy that assumes conflicts are rare. Instead of locking rows up-front, transactions proceed and validate at commit time, improving throughput when contention is low.
Given Data / Assumptions:
Concept / Approach:
Optimistic locking avoids holding locks during long business logic phases. It typically uses a version/timestamp check at update time. If another transaction has changed the row, the update fails and must be retried. The benefit is reduced blocking and better scalability under low-conflict patterns.
Step-by-Step Solution:
1) Read the data and carry a version value (e.g., row_version = 12).2) Perform processing without locking the row.3) On update, check that the current row_version is still 12.4) If unchanged, update and increment the version; if changed, detect conflict and retry.5) Locks (or write latches) are acquired only at the final write phase, not throughout.Verification / Alternative check:
Empirical performance studies show optimistic techniques excel when conflicts are rare because they minimize blocking and deadlocks, though they may degrade with frequent conflicts due to retries.
Why Other Options Are Wrong:
Common Pitfalls:
Final Answer:
The lock is obtained only when the transaction is ready to write or commit changes.
Discussion & Comments