Google App Engine& JDO:事务未写入数据存储区

时间:2013-05-06 07:45:55

标签: google-app-engine transactions google-cloud-datastore jdo

我们正在使用带有JDO2.3的GAE并拥有以下代码:

public void submit_job (HttpSession session, BlobKey blobKey) throws Exception {
  // START TRANSACTION
  PersistenceManager pm = PMF.get().getPersistenceManager();
  Transaction tx = pm.currentTransaction();
  tx.begin();

  // GET JOB AND MEMBER FROM DATASTORE
  Key jobKey = (Key)session.getAttribute("jobkey");
  String userName = session.getAttribute("username").toString();

  Job job = pm.getObjectById(Job.class, jobKey);
  Member m = pm.getObjectById(Member.class, username);

  // STORE JOB INFORMATION IN DATASTORE
  Date now = new Date();
  job.caricature = blobKey;
  job.whenSubmitted = now;
  job.whenFinished = now;
  pm.makePersistent(job);
  pm.flush();
  log.warning("submit_job: updating job " + job.key);

  // UPDATE MEMBER INFORMATION
  m.numSubmittedJobs++;
  pm.makePersistent(m);
  pm.flush();    
  log.warning("submit_job: updating user " + username);

  // COMPLETE TRANSACTION
  tx.commit();
  pm.close();
  log.warning("transaction completed? " + !tx.isActive());
}

症状:

  • 没有抛出任何异常,一切正常
  • 最后,交易正确完成(“交易完成?真”)
  • 以下查询可以从Member和Job对象中读取更新的数据

BUT

  • 信息未显示在数据存储区管理器中
  • 重新启动实例后,JDO对象具有旧值。所有的变化都很糟糕。

这是我们的jdoconfig.xml:

<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

   <persistence-manager-factory name="transactions-optional">
       <property name="javax.jdo.PersistenceManagerFactoryClass"
           value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
       <property name="javax.jdo.option.ConnectionURL" value="appengine"/>
       <property name="javax.jdo.option.NontransactionalRead" value="true"/>
       <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
       <property name="javax.jdo.option.RetainValues" value="true"/>
       <property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
       <property name="datanucleus.appengine.singletonPMFForName" value="true"/>
       <property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true"/>
   </persistence-manager-factory>
</jdoconfig>

和persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
        </properties>
    </persistence-unit>
</persistence>

感谢任何帮助,我们已经在这个主题上付出了很多努力,但还是无法解决它。

谢谢!

1 个答案:

答案 0 :(得分:1)

所以你要设置一个可持久化类的公共字段,并以某种方式期望持久性机制知道这一点。根据JDO规范,您应该使用setter,或者将设置为PersistenceAware的类注释。

并查看日志,因为它告诉你发生了什么