如何启用Objectify XA事务? [英] How to enable Objectify XA transaction?

查看:129
本文介绍了如何启用Objectify XA事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在相同类型的实体 Profile 之间实现友谊功能。这种实体类型是根(非父)实体。
个人资料包含名为好友的< Set< Ref<个人资料>> 字段,并且它的getter getFriends()

I'm implementing friendship functionality between entities of same type Profile. This entity kind is root (non parent) entity. A Profile has a Set<Ref<Profile>> field named friends and it's getter getFriends().

这里的代码:

Here the code:

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;
}

这段代码给了我一个:

This code give me a:

java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG

即使Objectify文档显示:

even if Objectify documentation says:


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

Objectify requires no special flags to enable cross-group transactions. If you access more than one entity group in a transaction, the transaction with be an XG transaction. If you do access only one, it is not. The standard limit of 5 EGs applies to all transactions.

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

So, why my transaction fails?

我的代码应该包含两个实体组(每个 Profile ),因此在5的限制之后。
TransactionOptions.Builder.withXG 文档,我应该先调用 TransactionOptions.Builder.withXG(true);
这个方法返回一个 TransactionOptions ,但我不知道传递它的方法!

My code should involve two entity groups (one for each Profile), so behind the limit of 5. Looking at TransactionOptions.Builder.withXG documentation, i should call TransactionOptions.Builder.withXG(true); before. This method returns a TransactionOptions but i don't know a method to pass it!

谢谢提前

推荐答案

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

Most likely, you're running a test case without the HRD enabled. You must do this explicitly in your LocalDatastoreServiceTestConfig; check the Local Unit Testing docs. If you are getting this message in the dev instance, make sure to check the "Use HRD" checkbox in the eclipse project preferences.

这篇关于如何启用Objectify XA事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆