对象化:“put()”和“put()”之间的延迟并通过“query()”找到对象
我正在使用 Objectify 将项目存储在数据库中,并希望强制“名称”字段的唯一性。在开始担心竞争条件之前,我从一个简单的实现开始......
以下是我将对象添加到数据库的方法:
Objectify ofy = ObjectifyService.begin();
if (ofy.query(Item.class).filter("name", name).count() == 0) {
Item newItem = new Item(name);
ofy.put(newItem);
}
如果我尝试快速插入一个对象几次,有时我将能够创建三个或过滤器找到现有对象并阻止保存新对象之前的四次。
这是在本地运行的 - 因此尚未部署到 Google App Engine。
我应该担心吗?我错过了一些明显的东西吗?我没有在 Item 类上显式启用缓存。
(开发环境为“Google Plugin for Eclipse”v1.5.2,Objectify 3.0)
I'm using Objectify to store items in a database, and want to enforce uniqueness on the "name" field. I'm starting with a trivial implementation, before I start worrying about race conditions...
Here's how I add the object to the database:
Objectify ofy = ObjectifyService.begin();
if (ofy.query(Item.class).filter("name", name).count() == 0) {
Item newItem = new Item(name);
ofy.put(newItem);
}
If I attempt to insert an object several times quickly, sometimes I'll be able to create three or four before the filter finds existing objects and prevents a new one being saved.
This is running locally - so hasn't been deployed to Google App Engine yet.
Should I be worried? Am I missing something obvious? I haven't explicitly enabled caching on the Item class.
(The dev environment is "Google Plugin for Eclipse" v1.5.2, Objectify 3.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种 HRD 行为是通过使用 @Parent 属性和祖先查询来解决的。另请参阅 http://code.google.com/p/objectify-appengine/wiki /AdvancedPatterns 和 http://code.google.com/appengine/docs/java/datastore/hr/overview.html
This HRD behavior is solved by using @Parent attribute and ancestor queries. See also http://code.google.com/p/objectify-appengine/wiki/AdvancedPatterns and http://code.google.com/appengine/docs/java/datastore/hr/overview.html
您是否尝试过通过提交强制事务?
http://code.google.com/p/objectify-appengine/wiki /IntroductionToObjectify#Transactions
Have you tried to force transaction by commit?
http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Transactions