2

我正在使用 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)

4

2 回答 2

2

这种 HRD 行为是通过使用 @Parent 属性和祖先查询来解决的。另请参阅http://code.google.com/p/objectify-appengine/wiki/AdvancedPatternshttp://code.google.com/appengine/docs/java/datastore/hr/overview.html

于 2011-11-25T15:22:05.260 回答
0

您是否尝试过通过提交来强制交易?

http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Transactions

于 2012-05-15T19:03:30.877 回答