Hi, let's have a look at this code:
$product = Products::findFirstById(1); $product->amount -= 1; sleep(5); if ($product->update()) echo 'ok'; else echo 'not ok';
and now lets assume this scenario:
- "User A" run this script, he gets stuck at sleep command
- One second later, "User B" does the same and also stuck at sleep()
- When script hits update(), then for both users script echoes "ok" but in actual database product amount is decreased only by 1!
I get why update operation works only for "User A", but how do i prevent this from happening when i don't even get an error from update()?
I have MySQL DB, InnoDB with default isolation levels.