使用Soap UI运行10个线程执行负载测试时,我遇到了死锁,这向我的Web服务发出10个请求,该请求调用OpenJPA实体管理器的persits方法。

然后,我执行单个请求,所有请求都结束了,但是在多线程环境中,它最终陷入了死锁。

我得到的错误是:

Deadlock found when trying to get lock; try restarting transaction {prepstmnt 12796448 UPDATE Assessment SET dateCompleted = ? WHERE id = ? [params=?, ?]} [code=1213, state=40001]


死锁崩溃的方法是这样的:

@Override
public AssessmentKey update(AssessmentKey assessmentKey) {
    OpenJPAEntityManager openJpaEntityMgr = OpenJPAPersistence.cast(entityManager);
    for (Assessment assessment : assessmentKey.getAssessments()) {
        if (!entityManager.contains(assessment) && !openJpaEntityMgr.isDetached(assessment)) {
            LOGGER.debug("Persisting {}", assessment);
            entityManager.persist(assessment);
        }
    }
    return entityManager.merge(assessmentKey);
}


Assessment是OpenJPA的实体,它只是包含JPA批注的普通实体。
如果您需要它的代码,请提出要求。

我了解here中提到了死锁是如何发生的,如何解决死锁。但是,如何在ORM中解决它们却找不到答案。

我的persistence.xml文件如下所示:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="com.groupgti.esb.online.tests">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>com.groupgti.esb.assessments.model.jpaimpl.Assessment</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <validation-mode>NONE</validation-mode>
        <properties>
            <property name="openjpa.Log" value="slf4j"/>
            <property name="openjpa.DataCache" value="false"/>
            <property name="openjpa.ReadLockLevel" value="none"/>
            <property name="openjpa.WriteLockLevel" value="pessimistic-write"/>
            <property name="openjpa.LockTimeout" value="10000"/>
            <property name="openjpa.RuntimeUnenhancedClasses" value="unsupported"/>
        </properties>
    </persistence-unit>
</persistence>


整个应用程序使用Spring,完成事务的类标记为@Transactional。如何解决ORM中的死锁问题?

编辑:

AssessmentKeyAssessment有关系:

@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "assessmentKey")
private List<Assessment> assessments = new ArrayList<Assessment>();


并且在Assessment AssessmentKey中看起来像这样:

@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@Index
@Column(nullable = false)
@ForeignKey
private AssessmentKey assessmentKey;

最佳答案

AssessmentKeyAssessmentOneToMany双向关系。OneToManyManyToOne批注都有CascadeType.PERSIST和其他。

要点是:

EntityManager保留Assessment时,AssessmentKey也将保留。您应该需要再次保存或合并AssessmentKey

我建议如下进行OR映射:

AssessmentKey.java

@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE},
           mappedBy = "assessmentKey", orphanRemoval=true)
private List<Assessment> assessments;


评估文件

@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH})
@JoinColumn(name = "ASSESSMENT_ID", referencedColumnName = "ID")
private AssessmentKey assessmentKey;


数据库操作

@Override
public AssessmentKey update(AssessmentKey assessmentKey) {
    OpenJPAEntityManager openJpaEntityMgr = OpenJPAPersistence.cast(entityManager);
    // there may be your operation
    return entityManager.merge(assessmentKey);
}


我假设当前Assessment对象的AssessmentKey值可能如下

On System                               |   In Existing Database
ID      Description                     |   ID      Descrition
1001    AAA         => No Changes       |   1001    AAA
1002    BBB         => Need to update   |   1002    BB
null    DDD         => Need to add      |   1003    CCC     => need to remove.


当EntityManager合并AssessmentKey时,如我在上面的数据结构中提到的,EntityManager将为Assessment完成所有过程。

注意:确保在orphanRemoval=true批注中使用OneToMany

07-24 12:36