Appengine - 在放入某些内容后立即查询数据库
在代码的一个地方,我做了这样的事情:
FormModel(.. some data here..).put()
下面的几行是我从数据库中选择的:
FormModel.all().filter(..).fetch(100)
我注意到的问题 - 有时提取没有注意到我刚刚添加的数据。
我的理论是,发生这种情况是因为我使用的是高复制存储,并且没有给它足够的时间来复制数据。但我怎样才能避免这个问题呢?
In one place of code I do something like this:
FormModel(.. some data here..).put()
And a couple lines below I select from the database:
FormModel.all().filter(..).fetch(100)
The problem I noticed - sometimes the fetch doesn't notice the data I just added.
My theory is that this happens because I'm using high replication storage, and I don't give it enough time to replicate the data. But how can I avoid this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非数据位于同一实体组中,否则无法保证数据是最新的数据(如果我 正确理解本节)。
Unless the data is in the same entity group there is no way to guarantee that the data will be the most up to data (If I understand this section correctly).
Shay 是对的:没有办法知道数据存储区何时准备好返回您刚刚输入的数据。
但是,一旦对
put
的调用成功完成,您可以保证最终会输入数据。这是大量信息,您可以使用它来解决此问题。当您从fetch
获取数据时,只需附加/插入您知道最终将位于其中的新实体即可!我认为,在大多数情况下,在每个请求的基础上执行此操作就足够了,但您可以做一些更强大的操作,使用 memcache 来覆盖所有请求(除了 memcache 失败的情况)。当然,困难的部分是弄清楚何时应该附加/插入哪些实体。必须采取这种解决方法是令人讨厌的,但对于像 HRD 这样复杂得惊人的事情来说,付出的代价相对较低。
Shay is right: there's no way to know when the datastore will be ready to return the data you just entered.
However, you are guaranteed that the data will be entered eventually, once the call to
put
completes successfully. That's a lot of information, and you can use it to work around this problem. When you get the data back fromfetch
, just append/insert the new entities that you know will be in there eventually! In most cases it will be good enough to do this on a per-request basis, I think, but you could do something more powerful that uses memcache to cover all requests (except cases where memcache fails).The hard part, of course, is figuring out when you should append/insert which entities. It's obnoxious to have to do this workaround, but a relatively low price to pay for something as astonishingly complex as the HRD.
来自 https://developers.google.com/appengine/docs/java/数据存储/事务#Java_Isolation_and_consistency
From https://developers.google.com/appengine/docs/java/datastore/transactions#Java_Isolation_and_consistency