【问题标题】:Hibernate exception: deadlock休眠异常:死锁
【发布时间】:2012-07-09 04:56:04
【问题描述】:

以下代码有时会出现异常“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。当我发现这是一个僵局。有人能帮忙吗?

【问题讨论】:

  • 问题在用户开始测试时被克隆。可能是因为几个用户同时开始测试?为每个用户创建单独的测试会话。

标签: hibernate db2 deadlock websphere-portal


【解决方案1】:
alternativeClone.setQuestion(questionClone); 

和

questionClone.setInternalAlternatives(alternativesClone);

似乎是dealock 因为这些行而出现。您在question 内的internalAlternatives 上添加列表,然后还在alternative 内设置question。

【讨论】:

  • 感谢您的回答!当我测试它时,它运行良好,没有抛出异常。但是,该应用程序已在使用中,异常出现在那里。会不会是几个人同时开始测试,而备选方案集被锁定的原因?
猜你喜欢
  • 1970-01-01
  • 2017-06-13
  • 2017-11-15
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 2014-02-22
相关资源
最近更新 更多