我遇到ConcurrentModificationException,同时对EntityManager.find或Query.getResultList方法的实体进行读取操作。我正在使用版本2.3.2 for EclipseLink。我从帖子https://bugs.eclipse.org/bugs/show_bug.cgi?id=229581获取线索后删除了显式的EntityManager刷新和刷新,并且行为没有变化,我仍然得到以下异常。
StrackTrace看起来像这样:
Caused by: java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at org.eclipse.persistence.internal.queries.InterfaceContainerPolicy.next(InterfaceContainerPolicy.java:274)
at org.eclipse.persistence.internal.queries.ContainerPolicy.next(ContainerPolicy.java:1116)
at org.eclipse.persistence.internal.helper.WriteLockManager.traverseRelatedLocks(WriteLockManager.java:199)
at org.eclipse.persistence.internal.helper.WriteLockManager.acquireLockAndRelatedLocks(WriteLockManager.java:144)
at org.eclipse.persistence.internal.helper.WriteLockManager.checkAndLockObject(WriteLockManager.java:449)
at org.eclipse.persistence.internal.helper.WriteLockManager.traverseRelatedLocks(WriteLockManager.java:213)
at org.eclipse.persistence.internal.helper.WriteLockManager.acquireLockAndRelatedLocks(WriteLockManager.java:144)
at org.eclipse.persistence.internal.helper.WriteLockManager.acquireLocksForClone(WriteLockManager.java:75)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.cloneAndRegisterObject(UnitOfWorkImpl.java:971)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.cloneAndRegisterObject(UnitOfWorkImpl.java:929)
at org.eclipse.persistence.internal.sessions.UnitOfWorkIdentityMapAccessor.getAndCloneCacheKeyFromParent(UnitOfWorkIdentityMapAccessor.java:181)
at org.eclipse.persistence.internal.sessions.UnitOfWorkIdentityMapAccessor.getFromIdentityMap(UnitOfWorkIdentityMapAccessor.java:120)
at org.eclipse.persistence.internal.sessions.IdentityMapAccessor.getFromIdentityMap(IdentityMapAccessor.java:380)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerExistingObject(UnitOfWorkImpl.java:3899)
at org.eclipse.persistence.queries.ObjectBuildingQuery.registerIndividualResult(ObjectBuildingQuery.java:400)
at org.eclipse.persistence.queries.ReadObjectQuery.registerResultInUnitOfWork(ReadObjectQuery.java:780)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkEarlyReturn(ObjectLevelReadQuery.java:802)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:781)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
at org.eclipse.persistence.queries.ReadObjectQuery.execute(ReadObjectQuery.java:418)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:734)
以下是我的类看起来如何在随机时间抛出ConcurrentModificationException,没有明确的步骤来重现该问题。还有其他方法。
@Stateless
@Local
@EJB(name = "java:global/framework/admin/AdminServicePersistence", beanInterface = AdminrServicePersistence.class)
public class AdminServicePersistenceImpl implements AdminServicePersistence {
@PersistenceContext(unitName = "AdminServicePU")
protected EntityManager em;
protected void setEntityManager(EntityManager em) {
this.em = em;
}
@Override
public AdminPE findAdminById(final Long id, final boolean refresh) throws AdminServiceModuleException {
try {
final TypedQuery<AdminPE> query = em.createNamedQuery(AdminPE.ADMIN_BY_ID, AdminPE.class);
query.setParameter("id", id);
if(refresh) {
query.setHint(QueryHints.REFRESH, HintValues.TRUE);
}
return executeSingleResultQuery(query);
} catch (Exception exc) {
throw new AdminServiceModuleException(AdminError.PERSISTENCE_QUERY_ERROR, exc);
}
}
}