I'm trying to write a transactional method for the app engine datastore but it's hard to test if it's working so I'm trying to validate my approach first. I have a post request that checks if a property is true, and if not true then do something else and set it to true.
def post(self):
key = self.request.get('key')
obj = db.get(key)
if obj.property is False:
update_obj(ojb.key()) // transactional method to update obj and set value to True
if obj.property is True:
// do something else
I am posting your code with some added comments
Consider transactions as a group of actions on an entity that will all execute or all fail.
Transactions ensure that anything inside them will remain consistent. If something alters an entity and becomes different than it was, the transaction will fail and then repeat again.
Another approach if I understand what you need:
As you see I've put all my checks inside a transaction. The above code, if run concurrently, will only increase the count once. Imagine it like a counter that counts how many times the
obj.property
has been set toTrue