0

我在ConcurrentModificationException应用程序的代码中遇到了一些问题:

   Caused by: Exception [EclipseLink-2004] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.ConcurrencyException
Exception Description: A signal was attempted before wait() on ConcurrencyManager. This normally means that an attempt was made to 
commit or rollback a transaction before it was started, or to rollback a transaction twice.
        at org.eclipse.persistence.exceptions.ConcurrencyException.signalAttemptedBeforeWait(ConcurrencyException.java:84)
        at org.eclipse.persistence.internal.helper.ConcurrencyManager.releaseReadLock(ConcurrencyManager.java:489)
        at org.eclipse.persistence.internal.identitymaps.CacheKey.releaseReadLock(CacheKey.java:392)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.cloneAndRegisterObject(UnitOfWorkImpl.java:1022)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.cloneAndRegisterObject(UnitOfWorkImpl.java:933)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkIdentityMapAccessor.getAndCloneCacheKeyFromParent(UnitOfWorkIdentityMapAccessor.java:197)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkIdentityMapAccessor.getFromIdentityMap(UnitOfWorkIdentityMapAccessor.java:125)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerExistingObject(UnitOfWorkImpl.java:3920)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerExistingObject(UnitOfWorkImpl.java:3874)
        at org.eclipse.persistence.mappings.ObjectReferenceMapping.buildUnitofWorkCloneForPartObject(ObjectReferenceMapping.java:109)
        at org.eclipse.persistence.mappings.ObjectReferenceMapping.buildCloneForPartObject(ObjectReferenceMapping.java:71)
        at org.eclipse.persistence.internal.indirection.UnitOfWorkQueryValueHolder.buildCloneFor(UnitOfWorkQueryValueHolder.java:56)
        at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiateImpl(UnitOfWorkValueHolder.java:161)
        at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiate(UnitOfWorkValueHolder.java:222)
        at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:88)

分析代码,我发现负责提供 EntityManager 的类:

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import weblogic.javaee.CallByReference;

@Stateful (name = "DAOFactoryRemoteBean", mappedName = "DAOFactoryRemoteBean")
@CallByReference
public class DAOFactoryRemoteImplBean implements DAOFactoryRemoteBean {

    @PersistenceContext(unitName = "server_unit")
    private EntityManager em;

    private static String getDAOName(Class<?> classeEntidade) {
        String result = classeEntidade.getName()
            .replace("org.networkmap.server.bean.",
            "org.networkmap.server.bean.dao.");
        result = result.concat("DAO");
        return result;
    }

    @Override
    public Object getDAO(Class<?> entityClass) {
        NetworkmapDao result = null;

        try {
           result = (NetworkmapDao) Class.forName(getDAOName(entityClass))
               .newInstance();
           result.setEntityManager(em);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
    }
}

有很多调用该getDAO方法的 EJB。还有很多应用程序的用户。我想它确实可能会导致并发问题。

EntityManger我对此没有很好的专业知识,但是根据互联网上的一些搜索,考虑到这不是线程安全的,似乎该代码没有好的做法。我被这个问题困住了:(

该应用程序使用 EclipseLink v2.4.2 API,在 Solaris OS 上的 Weblogic 10.3.5 上运行并且只有一个持久单元声明在persistence.xml.

请有人帮助我。

4

1 回答 1

2

您是否使用具有不同线程的相同 EntityManager?如果是这样,那就不要那样做。

否则,请包括完整的异常堆栈跟踪。

什么是“result.setEntityManager(em);” 做?

于 2013-08-08T13:19:23.633 回答