1

以下代码中有时会出现异常“org.hibernate.exception.LockAcquisitionException: could not insert” :

 Question questionClone;
 try {
    questionClone = (Question) question.clone();
 } catch (CloneNotSupportedException e) {
    throw WrappedException.wrap(e);
 }
 questionClone.setCategory(question.getCategory());
 questionClone.setOriginal(false);
 logger.trace("Saving questionClone " + questionClone + " start");
 hibernateSession.save(questionClone);
 logger.trace("Saving questionClone " + questionClone + " end");

当 questionClone 被保存时。这是问题的克隆方法:

public Object clone() throws CloneNotSupportedException {
    Question questionClone = (Question) super.clone();

    questionClone.category = null;

    List<Alternative> alternativesClone = new ArrayList<Alternative>(getInternalAlternatives().size());
    int index = 0;
    for (Iterator<Alternative> iterator = getInternalAlternatives().iterator(); iterator.hasNext();) {
        Alternative alternative = iterator.next();
        Alternative alternativeClone = (Alternative) alternative.clone();
        alternativeClone.setQuestion(questionClone);
        alternativeClone.setIndex(index);
        alternativesClone.add(alternativeClone);
        ++index;
    }
    questionClone.setInternalAlternatives(alternativesClone);

    return questionClone;
}

和替代方法的克隆方法:

public Object clone() throws CloneNotSupportedException {
    Alternative alternativeClone = (Alternative) super.clone();

    alternativeClone.index = 0;
    alternativeClone.question = null;

    return alternativeClone;
}

问题的休眠映射包含以下内容:

<list name="internalAlternatives" inverse="true" cascade="all-delete-orphan">
   <cache usage="read-write"/>
   <key column="QUESTION_ID"/>
   <list-index column="INDEX"/>
   <one-to-many class="Alternative"/>
</list>

异常表明它无法插入替代项,并且由以下原因引起:com.ibm.db2.jcc.am.SqlTransactionRollbackException:DB2 SQL 错误:SQLCODE=-911,SQLSTATE=40001,SQLERRMC=2,DRIVER=4.14.88。当我发现这是一个僵局。有人能帮忙吗?

4

1 回答 1

1
alternativeClone.setQuestion(questionClone); 

questionClone.setInternalAlternatives(alternativesClone);

似乎是dealock因为这些线而发生的。internalAlternatives你在你的里面添加列表question,然后也在question里面设置alternative

于 2012-07-09T05:07:32.650 回答