How to enable transaction Objectify XA?

I am implementing friendship functions between objects of the same type Profile

. This entity type is the root (non-parent) object. Profile has a field Set<Ref<Profile>>

named friends

and getter getFriends()

.

Here's 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 gives me:

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

      

even if the documentation for the object states:

Objectify does not require any special flags to enable cross-group transactions. If you are accessing multiple entities in a transaction, transaction XG. If you make this access only one, it is not. The standard 5 EG limit applies to all trades.

So why is my transaction failing?

My code should include two groups of entities (one for each Profile

), so it's over the limit of 5. After looking at the documentation TransactionOptions.Builder.withXG

, I should call TransactionOptions.Builder.withXG(true);

earlier. This method returns TransactionOptions

, but I don't know how to pass it!

Thank you in advance

+3


source to share


2 answers


Objectify always includes XG transactions if the environment supports it.



Chances are you are running the test case without HRD enabled. You must do this explicitly in your LocalDatastoreServiceTestConfig; check local testing docs. If you get this message in your dev instance, be sure to check the Use HRD checkbox in your eclipse project settings.

+6


source


Make sure you have enabled HRD in your local AppEngine for testing using the VM flag:



-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=1

      

0


source







All Articles