title: Deadlock in ORM when using OpenJPA
body: <p>I am getting a deadlock when performing the load testing with <code>Soap UI</code> running 10 every thread makes 10 request to my web service which invokes the persits method of OpenJPA entity manager.</p>
  
  <p>Then I am performing the single requests, all ends well, but in multithreaded environment it ends up with deadlocks.</p>
  
  <p>The error I am getting is:</p>
  
  <pre><code>Deadlock found when trying to get lock; try restarting transaction {prepstmnt 12796448 UPDATE Assessment SET dateCompleted = ? WHERE id = ? [params=?, ?]} [code=1213, state=40001]
  </code></pre>
  
  <p>The method which is crashing with a deadlock is this:</p>
  
  <pre><code>@Override
  public AssessmentKey update(AssessmentKey assessmentKey) {
      OpenJPAEntityManager openJpaEntityMgr = OpenJPAPersistence.cast(entityManager);
      for (Assessment assessment : assessmentKey.getAssessments()) {
          if (!entityManager.contains(assessment) &amp;&amp; !openJpaEntityMgr.isDetached(assessment)) {
              LOGGER.debug("Persisting {}", assessment);
              entityManager.persist(assessment);
          }
      }
      return entityManager.merge(assessmentKey);
  }
  </code></pre>
  
  <p><code>Assessment</code> is an entity of OpenJPA, it is just a normal entity containing JPA annotations.
  If you'll need the code of it, ask for it.</p>
  
  <p>I understood how deadlocks happening, how to solve them is mentioned in <a href="https://stackoverflow.com/questions/2332768/how-to-avoid-mysql-deadlock-found-when-trying-to-get-lock-try-restarting-trans">here</a>. But how to solve them in ORM I am not able to find an answer.</p>
  
  <p>My <code>persistence.xml</code> file looks like this:</p>
  
  <pre><code>&lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"&gt;
      &lt;persistence-unit name="com.groupgti.esb.online.tests"&gt;
          &lt;provider&gt;org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider&gt;
          &lt;class&gt;com.groupgti.esb.assessments.model.jpaimpl.Assessment&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
          &lt;validation-mode&gt;NONE&lt;/validation-mode&gt;
          &lt;properties&gt;
              &lt;property name="openjpa.Log" value="slf4j"/&gt;
              &lt;property name="openjpa.DataCache" value="false"/&gt;
              &lt;property name="openjpa.ReadLockLevel" value="none"/&gt;
              &lt;property name="openjpa.WriteLockLevel" value="pessimistic-write"/&gt;
              &lt;property name="openjpa.LockTimeout" value="10000"/&gt;
              &lt;property name="openjpa.RuntimeUnenhancedClasses" value="unsupported"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Whole application uses <code>Spring</code> and the classes in which transactions are done is marked <code>@Transactional</code>. How to solve this deadlock issue in ORM?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The <code>AssessmentKey</code> has a relation to <code>Assessment</code>:</p>
  
  <pre><code>@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "assessmentKey")
  private List&lt;Assessment&gt; assessments = new ArrayList&lt;Assessment&gt;();
  </code></pre>
  
  <p>And in the <code>Assessment</code> <code>AssessmentKey</code> looks like this:</p>
  
  <pre><code>@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
  @Index
  @Column(nullable = false)
  @ForeignKey
  private AssessmentKey assessmentKey;
  </code></pre>