我对数据存储读取的一致性不强,例如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读取返回强一致性结果?
怎么了?还是预期的行为?
最佳答案
您的实体已被覆盖。日志发布时间可能与对象更新时间不同。
您需要从事务内部调用getById()
。如此处所述(来自Objectify的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;
}
});