views:

38

answers:

2

I store groups of entities in the google app engine Data Store with the same ancestor/parent/entityGroup. This is so that the entities can be updated in one atomic datastore transaction.

The problem is as follows:

  1. I start a db transaction
  2. I update entityX by setting entityX.flag = True
  3. I save entityX
  4. I query for entity where flag == True. BUT, here is the problem. This query does NOT return any results. It should have returned entityX, but it did not.

When I remove the transaction, my code works perfectly, so it must be the transaction that is causing this strange behavior.

Should updates to entities in the entity group not be visible elsewhere in the same transaction?

PS: I am using Python. And GAE tells me I can't use nested transactions :(

A: 

Looks like you are not doing a commit on the transaction before querying

  1. start a db transaction
  2. update entityX by setting entityX.flag = True
  3. save entityX
  4. COMMIT TRANSACTION
  5. query for entity where flag == True. BUT, here is the problem. This query does NOT return any results. It should have returned entityX, but it did not.

In a transaction, entities will not be persisted until the transaction is commited

naikus
Probably. Trouble is that I can't use nested transactions. So I have to use two transactions, so that the first one is committed, but that defeats the purpose of trying to have a single atomic update :/
willem
Thanks naikus. As far as I'm aware though, the python version of the GAE API does not provide a way to commit a transaction half-way, since there is no transaction object that you pass around. I assume you are using the Java version?
willem
You are right, but in that case why would you want to query the entity before commiting? In this case you already have the most updated state of that entity.
naikus
Makes sense... I might have to re-think my code a little...
willem
@willem You are welcome, and as you rightly guessed I am using GAE Java :)
naikus
+3  A: 

App Engine's transactions are designed that way, ie reads within a transaction see a snapshot as of the beginning of the transaction, so they don't see the result of earlier writes within the transaction:

http://code.google.com/appengine/docs/python/datastore/transactions.html#Isolation_and_Consistency

Saxon Druce
Thanks Saxon. Now I know that transactions are specifically designed that way. I'll adapt my code. Thanks!
willem