如何启用Objectify XA交易?

时间:2013-02-06 13:45:56

标签: google-app-engine transactions objectify

我正在相同类型Profile的实体之间实现友谊功能。此实体类型是root(非父)实体。 个人资料中有一个名为Set<Ref<Profile>>的{​​{1}}字段,而且它是getter friends

这里是代码:

getFriends()

这段代码给了我一个:

public boolean makeFriends(final Profile profile1, final Profile profile2) {
    final Ref<Profile> profileRef1 = Ref.create(profile1);
    final Ref<Profile> profileRef2 = Ref.create(profile2);

    boolean result = false;

    // test to avoid useless transaction
    if (!profile1.getFriends().contains(profileRef2) && !profile2.getFriends().contains(profileRef1)) {
        // add to friends (Set<Ref<Profile>>) the Ref of each other
        result = ofy().transact(new Work<Boolean>() {
            @Override
            public Boolean run() {
                profile1.getFriends().add(profileRef2);
                profile2.getFriends().add(profileRef1);
                ofy().save().entities(profile1, profile2).now();
                return true;
            }
        });

    }
    return result;
}

即使Objectify文档说:

  

Objectify不需要特殊标志来启用跨组   交易。如果您访问a中的多个实体组   交易,交易是一个XG交易。如果你这样做   只访问一个,它不是。 5个EG的标准限制适用于所有   交易。

那么,为什么我的交易失败了?

我的代码应该涉及两个实体组(每个java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG 一个),因此超过了5的限制。 查看Profile文档,我应该先致电TransactionOptions.Builder.withXG。 此方法返回TransactionOptions.Builder.withXG(true);,但我不知道传递它的方法!

提前致谢

2 个答案:

答案 0 :(得分:6)

如果环境支持,Objectify总是打开XG事务。

最有可能的是,您正在运行未启用HRD的测试用例。您必须在LocalDatastoreServiceTestConfig中明确地执行此操作;检查本地单元测试文档。如果您在开发实例中收到此消息,请确保选中eclipse项目首选项中的“使用HRD”复选框。

答案 1 :(得分:0)

确保使用VM标志激活本地AppEngine中的HRD进行测试:

-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=1