使用getById()获取数据存储区的强一致性读取

时间:2015-04-06 12:00:05

标签: java google-app-engine objectify google-cloud-datastore

我对数据存储读取没有像here那样强烈的一致性。

代码(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)返回强一致性结果吗?

有什么问题?或者这是预期的行为?

1 个答案:

答案 0 :(得分:1)

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

您需要从交易中调用getById()。正如这里所解释的(来自Objectify&#w; wiki):

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;
    }
});