One deposit is lost. Which change prevents it?
-- two sessions run this at the same time SELECT bal FROM acct WHERE id = 1; -- both read 100 UPDATE acct SET bal = 110 WHERE id = 1; -- app wrote 100+10
- Add an index on
acct.id - Read with
SELECT DISTINCT - Run each step in autocommit
- Use
SET bal = bal + 10
Computing the new value in the app causes a lost update; an atomic
Play SQL & Databases ▸
bal = bal + 10 (or SELECT ... FOR UPDATE) serializes the writes.