3

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

even if Objectify documentation says:

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?

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!

Thanks in advance

4

2 に答える 2

6

環境が XG トランザクションをサポートしている場合、Objectify は常に XG トランザクションをオンにします。

ほとんどの場合、HRD を有効にせずにテスト ケースを実行しています。これは LocalDatastoreServiceTestConfig で明示的に行う必要があります。Local Unit Testing のドキュメントを確認してください。開発インスタンスでこのメッセージが表示される場合は、Eclipse プロジェクトの設定で [HRD を使用] チェックボックスをオンにしてください。

于 2013-02-06T15:25:45.080 に答える
0

VM フラグを使用して、テスト用にローカル AppEngine で HRD を有効にしたことを確認します。

-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=1
于 2014-02-17T17:34:03.827 に答える