Search code examples
javagoogle-app-enginejpapersistence

Where do I set TransactionOptions with JPA / Google App Engine?


I use JPA within GAE to persist objects.

javax.persistence.PersistenceException: Illegal argument
    at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:298)
    at org.datanucleus.api.jpa.JPAEntityTransaction.commit(JPAEntityTransaction.java:122)
Caused by: java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXGfound both 
Element {
  type: "Event"
  id: 0x11000000000000
}
and Element {
  type: "User"
  id: 0x14000000000000
}

There is a bidirectional Many-To-Many relationship between Event and User. When I insert a new Event and bound it to existing Users inside a transaction, I get the above exception. From the error message I see that I need to tweak TransactionOptions. Where can I set these options?


Solution

  • Set the following property in persistence.xml:

    <property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true" />
    

    There is currently no way to enable XG transactions for an individual JDO or JPA transaction. Instead, you specify this for all transactions. [docs]