Closed leicmi closed 3 years ago
Can you explain this a little more? I dont quite understand it. Suppose someone updated a row concurrently. In the code you only try the 'update' part multiple times. But then it will fail for all attempts because the "IF amount = %s;" will fail for the outdated value of 'amount', right?
Can you explain this a little more? I dont quite understand it. Suppose someone updated a row concurrently. In the code you only try the 'update' part multiple times. But then it will fail for all attempts because the "IF amount = %s;" will fail for the outdated value of 'amount', right?
res = ... // get amount from database
for attempt in range(5):
amount = res.amount
new_amount = amount + number
res = ... // UPDATE stock SET amount = {new_amount} IF amount = {amount}
if res.applied:
return True
return False
The interesting thing here is that the UPDATE query with IF returns the value of the failing column "amount" (accessed at the beginning of the next loop iteration) if the condition fails (res.appliedd==False).
hmm, looking again at the link, seems I was wrong? The documentation on the behavior for IF is really lacking...
Found some documentation that explains this behavior with an example: https://docs.datastax.com/en/developer/python-driver/3.23/lwt/#named-tuple-factory-default
>>> session.execute("UPDATE t SET v = 1, x = 2 WHERE k = 0 IF v =0")
Row(applied=True)
>>> session.execute("UPDATE t SET v = 1, x = 2 WHERE k = 0 IF v =0 AND x = 1")
Row(applied=False, v=1, x=2)
I think this will work then, but can you first test it?
closed due to the project having ended
This will make the service call Cassandra multiple times if someone else concurrently updated our row. Remember that rollbacks are important for consistency with SAGAs.