The GAE docs warn:
Whenever possible, make your datastore transactions idempotent so that if you repeat a transaction, the end result will be the same.
Suppose I want to transfer an amount of money between two people:
class User(ndb.Model):
balance = ndb.IntegerProperty(default=0)
@ndb.transactional(xg=True)
def transfer(from_key, to_key, amount)
from = from_key.get()
to = to_key.get()
from.balance -= amount
to.balance += amount
ndb.put_multi([from, to])
Since this isn't idempotent, it could happen more than once and cause a problem. I'd like to refactor this to ensure that the transaction is idempotent.
This answer suggests a solution:
To solve the problem, you can make the transaction idempotent by creating a "transaction Key" and recording that key in a new entity as part of the transaction. The second transaction can check for that transaction key, and if found, will do nothing. The transaction key can be deleted once you're satisfied that the transaction completed, or you give up retrying.
But I don't understand how to implement it.
Could someone explain how to make this transaction idempotent?