通过按键加载保存的实体实现数据存储强一致性
Datastore strong consistency by loading saved entity by key
如果我将一个实体保存到数据存储中,然后在通过它的键加载该实体之后,程序会等待并确保强一致性吗?
thing.setValue(newValue);
ofy().save().entity(thing).now();
Thing updatedThing = ofy().load().key(thingKey).now();
updatedThing
是否包含新值?
当我想更新实体时,这是确保强一致性的好方法吗?
是的,键查找在数据存储区中始终保持一致。从table开头的Eventual Consistency in Cloud Datastore:
[Lookup by key][2] (get()) Strong consistency
但是有一个问题 - 如果您在同一事务中同时执行写入和读取,则读取将看不到写入的信息。来自 Isolation and consistency:
This consistent snapshot view also extends to reads after writes
inside transactions. Unlike with most databases, queries and gets
inside a Cloud Datastore transaction do not see the results of
previous writes inside that transaction. Specifically, if an entity is
modified or deleted within a transaction, a query or get returns the
original version of the entity as of the beginning of the transaction, or nothing if the entity did not exist then.
如果我将一个实体保存到数据存储中,然后在通过它的键加载该实体之后,程序会等待并确保强一致性吗?
thing.setValue(newValue);
ofy().save().entity(thing).now();
Thing updatedThing = ofy().load().key(thingKey).now();
updatedThing
是否包含新值?
当我想更新实体时,这是确保强一致性的好方法吗?
是的,键查找在数据存储区中始终保持一致。从table开头的Eventual Consistency in Cloud Datastore:
[Lookup by key][2] (get()) Strong consistency
但是有一个问题 - 如果您在同一事务中同时执行写入和读取,则读取将看不到写入的信息。来自 Isolation and consistency:
This consistent snapshot view also extends to reads after writes inside transactions. Unlike with most databases, queries and gets inside a Cloud Datastore transaction do not see the results of previous writes inside that transaction. Specifically, if an entity is modified or deleted within a transaction, a query or get returns the original version of the entity as of the beginning of the transaction, or nothing if the entity did not exist then.