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

眉间皱痕 提交于 2019-12-01 19:17:43

问题


I use JDO within GAE to batch persist objects using the following method:

public void makePersistent(PersistenceManager pm,
        List<Regeling> makePersistent) {        
    Transaction tx = pm.currentTransaction(); 
    try {
        // Start the transaction
        tx.begin();
        // Persist to the datastore
        // pm.makePersistentAll(makePersistent);
        for (int i = 0; i < makePersistent.size(); i += BATCH_SIZE) {
            int last = i + BATCH_SIZE;
            last = last > makePersistent.size() ? makePersistent.size()
                    : last;
            pm.makePersistentAll(makePersistent.subList(i, last));
            pm.flush();
            System.out.println("Made "+last+" items persistent.");
        }
        // Commit the transaction, flushing the object to the datastore
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (tx.isActive()) {
            // Error occurred so rollback the transaction
            System.out.println("Rolling back transaction");
            tx.rollback();
        }
        pm.close();
    }
}

This breaks:

javax.jdo.JDOUserException: One or more instances could not be made persistent
    at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistentAll(JDOPersistenceManager.java:791)
    ...
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
NestedThrowablesStackTrace:

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

 Element {
  type: "PersistentServiceResultaat$RegelingInfoLijst$Regeling"
  name: "BWBR0001821"
}
 and 

 Element {
  type: "PersistentServiceResultaat$RegelingInfoLijst$Regeling"
  name: "BWBR0001822"
}

So I try to set these options:

TransactionOptions ops = TransactionOptions.Builder.withXG(true);

But I can't find a method that takes a TransactionOptions object. Where can I set these options?


回答1:


Set it in jdoconfig.xml:

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


来源:https://stackoverflow.com/questions/19464274/where-do-i-set-transactionoptions-with-jdo-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!