Our component store has a stock table. The application reads the quantity, checks that it is greater than zero, and then writes back quantity - 1. Under load two requests sometimes read the same value, say 1, and both succeed, so one item is sold twice.
I wrapped the two statements in BEGIN and COMMIT, but it still happens occasionally. What does a transaction actually guarantee here, and what is the correct pattern?




