I'm reading over this article on handling concurrency in django
The post talk about the following example:
- User A fetches the account — balance is 100$.
- User B fetches the account — balance is 100$.
- User B withdraws 30$ — balance is updated to 100$ — 30$ = 70$.
- User A deposits 50$ — balance is updated to 100$ + 50$ = 150$.
I tried to reproduce with a UserBalance model with balance column that use IntegerField(), as the article said i need to use select_for_update() to avoid this issue
def test_con(id):
with transaction.atomic():
user_balance = UserBalance.objects.select_for_update().get(pk=int(id))
sleep(60)
user_balance.balance -= 30
user_balance.save()
So this transaction is lock for 60 seconds
In the first shell i go into manage.py shell to call it
In another shell after i execute test_con(5) i ran the following :
>>> user_balance = UserBalance.objects.get(pk=5)
>>> user_balance.balance += 50
>>> user_balance.save()
>>> print(str(user_balance.balance))
the second shell wait till the first shell test_con(5) transaction is completed(as expected) and return the balance but it return 150 ? shouldn't it be 120 ?
I'm confused about why this locking mechanic doesn't work