2012-07-09 75 views
1

例外 「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> 

異常狀態,它不能插入替代並導致:com.ibm.db2.jcc.am.SqlTransactionRollbackException:DB2 SQL錯誤:SQLCODE = -911,SQLSTATE = 40001,SQLERRMC = 2,DRIVER = 4.14.88。當我發現這是一個僵局。有人能幫忙嗎?

+0

用戶在開始測試時克隆了這些問題。這可能是由於幾個用戶同時開始測試嗎?爲每個用戶創建單獨的測試會話。 – 2012-07-09 05:52:22

回答

1
alternativeClone.setQuestion(questionClone); 

questionClone.setInternalAlternatives(alternativesClone); 

好像是dealock發生監守這些線的。您在question之內添加internalAlternatives的列表,然後在alternative之內也設置question

+0

謝謝你的答案!當我測試它時,它工作正常,沒有拋出異常。然而,該應用程序已在使用中,並且出現異常。這可能是幾個人同時開始測試並且一組替代品被鎖定的原因嗎? – 2012-07-10 08:22:14