简体   繁体   English

使用Google App Engine数据存储区添加实体时的行为不一致

[英]Inconsistent behavior when adding an Entity using Google App Engine datastore

I'm working on a Google App Engine project that's written in Java. 我正在使用Java编写的Google App Engine项目。

Whenever I add an entity into the datastore I use a transaction: 每当我将实体添加到数据存储区时,我都会使用事务:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction transaction = datastore.beginTransaction();     
datastore.put(entity);
transaction.commit();

I then count the number of entities that are in the datastore: 然后,我计算数据存储区中的实体数:

Query query = new Query(kind);
query.setFilter(filter);

PreparedQuery preparedQuery = datastore.prepare(query); 
int count = preparedQuery.countEntities(FetchOptions.Builder.withDefaults());

System.out.println("count = " + count);

I've noticed that sometimes the datastore can be inconsistent. 我注意到有时数据存储区可能不一致。 Like sometimes I'll add an entity, but the count doesn't show up. 就像有时候我会添加一个实体,但是没有显示计数。 If I then add another entity the count goes up by 2. 如果再添加另一个实体,则计数增加2。

Why does this happen? 为什么会这样? How do I stop it from happening? 我如何阻止它发生?

You are experiencing the effects of "Eventual Consistency", which is a side effect of using the High Replication Datastore. 您正在体验“最终一致性”的影响,这是使用高复制数据存储的副作用。 Basically, you are calling the query before the data has time to replicate across data centres. 基本上,您是在数据有时间跨数据中心复制之前调用查询。 You can avoid this, by using "Ancestor Queries", which always results in "Strongly Consistent" queries. 您可以通过使用“祖先查询”避免这种情况,该查询始终会导致“严格一致”查询。 Suggest you read this , which explains it in detail. 建议您阅读 ,这将对其进行详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM