I have a field on a User table that holds the account balance for the user. Users can perform a lot of actions with my service that will result in rapid changes to their balance.
I m trying to use mysql s serializable isolation level to make sure that multiple user actions will not update the value incorrectly. (Action A and action B simultaneously want to deduct 1 dollar from the balance.) However, I m getting a lot of deadlock errors.
我如何在不打破所有这些僵局的情况下正确这样做,并且仍然保持平衡。
simple schema: user has an id and a balance.
im using doctrine, so i m doing something like the following:
$con->beginTransaction();
$tx = $con->transaction;
$tx->setIsolation( SERIALIZABLE );
$user = UserTable::getInstance()->find($userId);
$user->setBalance($user->getBalance() + $change);
$user->save();
$con->commit();