Does an AppEngine Transaction need to perform a get and a put operation for it to be useful?
google-app-engine, python, transactions
Solution
To answer your question in one word: yes, your second example is the way to do it. In the boundaries of a transaction, you get some data, change it, and commit the new value.
Your first one is not wrong, though, because you don't read from `obj`. So even though it might not have the same value that the earlier get returned, you wouldn't notice. Put another way: as written, your examples aren't good at illustrating the point of a transaction, which is usually called "test and set". See a good Wikipedia article on it here: http://en.wikipedia.org/wiki/Test-and-set
More specific to GAE, as defined in GAE docs, a transaction is:
a set of Datastore operations on one or more entities. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all of the operations in the transaction are applied, or none of them are applied.
which tells you it doesn't have to be just for test and set, it could also be useful for ensuring the batch commit of several entities, etc.
Problem
Two code examples (simplified): .get outside the transaction (object from .get passed into the transactional function) ``` @db.transactional def update_object_1_txn(obj, new_value): obj.prop1 = new_value return obj.put() ``` .get inside the transaction ``` @db.transactional def update_object2_txn(obj_key, new_value): obj = db.get(obj_key) obj.prop1 = new_value return obj.put() ``` Is the first example logically sound? Is the transaction there useful at all, does it provide anything? I'm trying to better understand appengine's transactions. Would choosing the second option prevent from concurrent modifications for that object?