如何启用Objectify XA交易?

Fuz*_*zzo 3 google-app-engine transactions objectify

我正在相同类型的实体之间实现友谊功能Profile.此实体类型是root(非父)实体.一个Profile有一个Set<Ref<Profile>>名为的字段friends,它是getter 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;
}
Run Code Online (Sandbox Code Playgroud)

这段代码给了我一个:

java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG
Run Code Online (Sandbox Code Playgroud)

即使Objectify文档说:

Objectify不需要特殊标志来启用跨组事务.如果您在事务中访问多个实体组,则该事务将成为XG事务.如果您只访问一个,则不是.5个EG的标准限制适用于所有交易.

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

我的代码应该涉及两个实体组(每个一个Profile),所以在5的限制之后.查看TransactionOptions.Builder.withXG文档,我应该TransactionOptions.Builder.withXG(true);之前调用.这个方法返回一个,TransactionOptions但我不知道传递它的方法!

提前致谢

sti*_*ure 6

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

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

  • 解决了`LocalDatastoreServiceTestConfig config = new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy();` (3认同)