views:

23

answers:

1

If I passed a list of key ids as an argument in a transaction, would the change associated with the first key in the list happen first? And if not, how do I specify the order that I want the changes to happen in?

As a concrete example, consider this code below from Google Docs Transactions--would changes to the first item in acc.key() happen first?

class Accumulator(db.Model):
    counter = db.IntegerProperty()

Docshttp://code.google.com/appengine/docs/python/datastore/transactions.html:
def increment_counter(key, amount):
    obj = db.get(key)
    obj.counter += amount
    obj.put()

q = db.GqlQuery("SELECT * FROM Accumulator")
acc = q.get()
db.run_in_transaction(increment_counter, acc.key(), 5)
+2  A: 

q.get() only returns the first result, so only one entity is updated in this code (or None).

Kind of the point of a transaction is that all the changes to the datastore happen "at the same time". I can't think of any situations where I would want multiple changes in a transaction to have an order - having an order implies that something is seeing a state of the data model where the transaction is only partly completed, and the purpose of transactions is to avoid that happening.

Even within the transaction, the changes don't occur sequentially, they all happen together at the end. If you put() an entity and then get() it inside the transaction then you don't see any changes, just the state of the datastore before the transaction.

Steve Jessop
Actually, calling get() inside a transaction on an entity you've already modified returns the unmodified entity - otherwise, this is all correct.
Nick Johnson
Thanks very much Nick, will correct.
Steve Jessop