我正在查询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侦听器。我应该保留这两个中的任何一个&#34;关闭&#34;陈述,它们是多余的吗?
答案 0 :(得分: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;
}
答案 1 :(得分: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
中添加或删除,调用addListener
或removeListener
?