I'm currently writing an API in which a user could transfer money from their account to a phone but I'm facing an issue which is the user can call the API more than once at the same time and ask for transferring the money. Normally the balance of his account wouldn't be correct after the calls so I searched a lot and found out that I can use atomic transactions and lock the database. Now if the user uses the API for example twice, one of them runs correctly but the other one gets django.db.utils.OperationalError: database is locked
error. Any idea how can I handle both of them correctly? (I could use a and wait until the database gets unlocked but I rather don't do that)
@transaction.atomic()
def withdraw(self, amount, phone, username):
property = transferLog.objects.filter(username=username).order_by('-time').select_for_update()[0]
if property.property >= int(amount):
self.username = username
self.property = property.property - int(amount)
self._from = username
self._to = phone
self.amount = int(amount)
self.time = str(datetime.datetime.now())
self.save()
return {'result': 'success', 'message': 'transfer complete', 'remaining': self.property}
else:
return {'result': 'error', 'message': 'not enough money'}
