Exception “org.hibernate.exception.LockAcquisitionException: could not insert” appears sometimes in the following code:
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");
when questionClone is saved. Here is the clone method for Question:
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;
}
And clone method for Alternative:
public Object clone() throws CloneNotSupportedException {
Alternative alternativeClone = (Alternative) super.clone();
alternativeClone.index = 0;
alternativeClone.question = null;
return alternativeClone;
}
Hibernate mapping of question contains this:
<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>
Exception states that it cannot insert an Alternative and caused by: com.ibm.db2.jcc.am.SqlTransactionRollbackException: DB2 SQL Error: SQLCODE=-911, SQLSTATE=40001, SQLERRMC=2, DRIVER=4.14.88. As I found out it was a deadlock. Can anyone help with this?
and
Seems to be the
dealockoccurs becuase of these lines. You are adding list oninternalAlternativesinside yourquestion, and then also settingquestioninsidealternative.