Two transactions read the same row and then write it back. Which measure prevents the lost update?
Correct answer
A. SELECT with FOR UPDATE, which holds a row lock until the transaction ends
22 views
A. SELECT with FOR UPDATE, which holds a row lock until the transaction ends
22 views
SELECT ... FOR UPDATE takes a row-level lock, so a concurrent transaction blocks at its read until the first one commits, which serialises the read-modify-write cycle. An atomic UPDATE t SET n = n + 1 avoids the pattern altogether by doing the arithmetic inside one statement. Raising the isolation level to SERIALIZABLE also works, at the cost of more aborts and retries.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is A? Ask here and get an explanation.