未使用 getById() 获取 Datastore 强一致性读取

Not getting Datastore strong consistency reads with getById()

我没有像 here 所说的那样在 Datastore 读取上获得强一致性。

代码(Objectify)。

a = getById() // ofy().load().type(this.clazz).id(id).now()

Begin transaction

   ...
   a.count++;
   putSync(a) // ofy().save().entity(entity).now();
   ... 

End transaction

请注意,即使在更新到 03:00:27 的 2818 和更新到 03:00:47 的 2819 之后,我也在 03:01:07 获得 2817 的计数器。

2015-04-06 03:01:07.582 /rest/x 200 127ms 3kb Java/1.7.0_75 module=x version=v3
2015-04-06 03:01:07.496 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: After getById() ... 2817
2015-04-06 03:01:07.547 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: Before putSync() ... 2818

2015-04-06 03:00:47.449 /rest/x 200 216ms 3kb Java/1.7.0_75 module=x version=v3
2015-04-06 03:00:47.339 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: After getById() ... 2818
2015-04-06 03:00:47.395 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: Before putSync() ... 2819

2015-04-06 03:00:27.227 /rest/x 200 189ms 3kb Java/1.7.0_75 module=x version=v3
2015-04-06 03:00:27.122 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: After getById() ... 2817
2015-04-06 03:00:27.174 [S~Primebus01/Transmissoes:v3.383369173292091449].<Stdout>: Before PutSync() ... 2818

此行为仅在一天中非常特定的时间发生。

不应该读取(按 id)return 强一致性结果?

怎么了?或者这是预期的行为?

您的实体正在被覆盖。日志发布时间可能与对象更新时间不同。

您需要从事务内部调用 getById()。如此处所述(来自 Objectify 的维基):

Thing th = ofy().transact(new Work<Thing>() {
    public Thing run() {
        Thing thing = ofy().load().key(thingKey).now();
        thing.modify();
        ofy().save().entity(thing);
        return thing;
    }
});