2

我正在查询 GAE 数据存储以检索实体列表(使用带有 sdk 1.8.3 的 dev gae):

public List<OT> getAll() {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query q = pm.newQuery();
    q.setClass(OT.class);
    try {
        return (List<OT>) q.execute();
    }
    finally {
        q.closeAll();
        pm.close();
    }
}

这总是在线ConcurrentModificationException上抛出pm.close()

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection.close(DatastoreConnectionFactoryImpl.java:146)
    at org.datanucleus.store.connection.ConnectionManagerImpl.closeAllConnections(ConnectionManagerImpl.java:181)
    at org.datanucleus.store.AbstractStoreManager$1.preClose(AbstractStoreManager.java:260)
    at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1112)
    at org.datanucleus.api.jdo.JDOPersistenceManager.internalClose(JDOPersistenceManager.java:359)
    at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.releasePersistenceManager(JDOPersistenceManagerFactory.java:1106)
    at org.datanucleus.api.jdo.JDOPersistenceManager.close(JDOPersistenceManager.java:343)

我正在使用以下依赖项:

    <dependency>
        <groupId>com.google.appengine.orm</groupId>
        <artifactId>datanucleus-appengine</artifactId>
        <version>2.0.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>datanucleus-core</artifactId>
        <version>3.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>datanucleus-api-jdo</artifactId>
        <version>3.1.3</version>
    </dependency>
    <dependency>
        <groupId>javax.jdo</groupId>
        <artifactId>jdo-api</artifactId>
        <version>[3.0, 4.0)</version>
    </dependency>
    <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>datanucleus-rdbms</artifactId>
        <version>3.1.3</version>
        <scope>runtime</scope>
    </dependency>

以及以下版本的 DN 增强器:

    <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>datanucleus-enhancer</artifactId>
        <version>3.1.1</version>
    </dependency>

我没有对集合中的任何对象或集合本身进行任何修改。事实上,即使集合为空,错误也会发生......知道我可能做错了什么吗?

额外的调试细节 我在 pm.close() 语句中进入了调试模式,发现在迭代监听器时,其中一个正在从监听器列表中删除自己:

447468239@qtp-1694835335-71@11277, prio=5, in group 'main', status: 'RUNNING'
  at org.datanucleus.store.connection.ConnectionManagerImpl$1.managedConnectionPostClose(ConnectionManagerImpl.java:247)
  at com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection.close(DatastoreConnectionFactoryImpl.java:147)
  at org.datanucleus.store.connection.ConnectionManagerImpl.closeAllConnections(ConnectionManagerImpl.java:181)
  at org.datanucleus.store.AbstractStoreManager$1.preClose(AbstractStoreManager.java:260)
  at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1112)
  at org.datanucleus.api.jdo.JDOPersistenceManager.internalClose(JDOPersistenceManager.java:359)
  at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.releasePersistenceManager(JDOPersistenceManagerFactory.java:1106)
  at org.datanucleus.api.jdo.JDOPersistenceManager.close(JDOPersistenceManager.java:343)
  at net.mycrub.jacasse.persistence.dao.GenericDAO.getAll(GenericDAO.java:43)

ConnectionManagerImpl.java:247 处的代码

                    // Remove this listener
                    mconn.removeListener(this);

这不是我自己添加的听众。它实际上是在Query.closeAll()声明期间添加的 DN 侦听器。我应该保留这两个“关闭”语句中的任何一个,它们是多余的吗?

4

2 回答 2

1

你有没有尝试过这样的事情?

public List<OT> getAll()
{
  List<OT> liResult = null;

  PersistenceManager pm = PMF.get().getPersistenceManager();
  Query q = pm.newQuery();
  try
  {
    q.setClass(OT.class);
    liResult = (List<OT>)q.execute();
  }
  finally
  {
    try
    {
      q.closeAll();
    }
    finally
    {
      pm.close();
    }
  }

  return liResult;
}
于 2013-09-19T08:33:03.773 回答
0

方法DatastoreManagedConnection#close()很简单

public void close() {
    for (ManagedConnectionResourceListener listener : listeners) {
        listener.managedConnectionPreClose();
    }
    // nothing to actually close
    for (ManagedConnectionResourceListener listener : listeners) {
        listener.managedConnectionPostClose();
    }
}

ConcurrentModificationException不能直接来自这里,因为此代码中没有任何内容修改listeners List. 是否有可能您有其他代码并行运行并添加或删除listeners、调用addListenerremoveListener

于 2013-09-17T16:00:09.530 回答