Hibernate异常:死锁

时间:2012-07-09 04:56:04

标签: hibernate db2 deadlock websphere-portal

异常“org.hibernate.exception.LockAcquisitionException:无法在以下代码中插入”有时

 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;
}

问题的Hibernate映射包含:

<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>

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

1 个答案:

答案 0 :(得分:1)

alternativeClone.setQuestion(questionClone); 

questionClone.setInternalAlternatives(alternativesClone);

由于这些行似乎是dealock。您要在internalAlternatives内的question内添加列表,然后在question内设置alternative