【发布时间】:2015-02-17 00:44:41
【问题描述】:
我将 Google App Engine 和 Datastore 与 objectify 一起使用。
我正在尝试为用户(Google 用户)创建一个帐户,因此我需要检查该用户是否存在,如果不存在,则为该用户创建一个帐户,但我面临的事实是,有时如果我向 createAccount API 方法发送垃圾邮件,则会创建两次帐户
@ApiMethod(name = "account.google.create")
public Account createGoogleAccount(final User user) throws OAuthRequestException {
if (user == null) {
throw new OAuthRequestException("createAccount: OAuthRequestException<User is not authenticated>");
}
Account alreadyExisting = RObjectifyService.getObjectify().load().type(Account.class).filter("accountId.GOOGLE", user.getUserId()).filter("email", user.getEmail()).first().now();
if (alreadyExisting != null) {
throw new OAuthRequestException("createAccount: OAuthRequestException<Account already exist>");
}
return RObjectifyService.getObjectify().transactNew(new Work<Account>() {
@Override
public Account run() {
Account account = AccountProvider.createAccountFromGoogleProvider(user);
RObjectifyService.save(account);
return account;
}
});
}
我读到我应该使用事务,但我不能,因为如果我在事务中这样做:
RObjectifyService.getObjectify().load().type(Account.class).filter("accountId.GOOGLE", user.getUserId()).filter("email", user.getEmail()).first().now()
我收到一个错误“事务中只允许祖先查询”,但我没有看到其他方法
这是正确的做法吗?
谢谢
【问题讨论】:
标签: google-app-engine google-cloud-datastore objectify