Eclipselink抛出ConcurrentModificationException

时间:2013-08-07 21:21:02

标签: java jpa ejb entitymanager

我在应用程序的代码中遇到一些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;
    }
}

有很多EJB调用getDAO方法。和应用程序的很多用户。我想这可能会导致并发问题。

我对此没有很好的专业知识,但是根据互联网上的一些搜索,考虑到EntityManger不是线程安全的,看起来代码没有好的做法。我坚持这个问题:(

该应用程序使用EclipseLink v2.4.2 API,通过Solaris O.S在Weblogic 10.3.5上运行。并且persistence.xml上只宣告了一个持久单位。

请有人帮帮我。

1 个答案:

答案 0 :(得分:2)

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

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

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