Reputation: 55
I'm working on a Google App Engine project that's written in Java.
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.
Why does this happen? How do I stop it from happening?
Upvotes: 1
Views: 101
Reputation: 5424
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.
Upvotes: 4