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 が保存されたとき。Question の clone メソッドは次のとおりです。

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

そして、Alternative の clone メソッド:

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>

例外は、オルタナティブを挿入できないことを示しており、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); 

and

questionClone.setInternalAlternatives(alternativesClone);

Seems to be the dealock occurs becuase of these lines. You are adding list on internalAlternatives inside your question, and then also setting question inside alternative.

于 2012-07-09T05:07:32.650 に答える