google app engine - GAE: Prevent inadvertent overwriting of data -
suppose have entity person 2 properties:
class person(ndb.model) payments = ndb.integerproperty(default=0) name = ndb.stringproperty()
i thinking update payments
in transaction not need transactions when updating name
.
however, seems in following scenario, lose value of payments
:
time | instance1 | instance2 (in transaction) ===================================================== || | person1 = key1.get() | || | person1.name = "john" | person1 = key1.get() || | [other stuff that] | person1.payments = 100 || | [takes time] | person1.put() || | person1.put() | || | \/
in scenario, seems instance1 overwrite payment amount written in instance2 , payment amount lost.
does mean need use transactions when updating name
make sure important data never lost?
or more generally, if using transactions update property of entity, should use transactions updates entity?
yes, lose new value of payments
in scenario. datastore has no concept of updating property value, entity overwritten.
the datastore api not distinguish between creating new entity , updating existing one. if object's key represents entity exists, put() method overwrites existing entity.
when both transactions, works fine. how plays out exactly.
when transaction starts, app engine uses optimistic concurrency control checking last update time entity groups used in transaction. upon commiting transaction entity groups, app engine again checks last update time entity groups used in transaction. if has changed since our initial check, exception thrown.
in particular case, when transaction in instance a
committed, app engine notices entity has been updated elsewhere since transaction started. transaction a
cancelled, , default retried.
Comments
Post a Comment