title: MySQL: Deadlock found when trying to get lock
body: <p>The next exception raises with this configuration:</p>
  
  <ul>
  <li>MySQL 5.7.10 </li>
  <li>Spring 4.0.5</li>
  <li>Hibernate 4.1.9 </li>
  <li>Atomikos 3.8.0 </li>
  <li>Spring ThreadPoolTaskExecutor between 10 and 20 threads</li>
  </ul>
  
  <p>I need Atomikos for transactions between two databases.</p>
  
  <p>The exception is:</p>
  
  <pre><code>javax.persistence.PersistenceException: org.hibernate.exception.LockAcquisitionException: Deadlock found when trying to get lock; try restarting transaction
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:273)
      at com.app.writer.jta.dao.CustomerDAOImpl.findCustomer(CustomerDAOImpl.java:35)
      at com.app.writer.dao.impl.JTAPDFWriterDAO.saveBean(JTAPDFWriterDAO.java:64)
      at com.app.writer.item.writer.PDFWriter.write(PDFWriter.java:17)
      at org.springframework.batch.item.support.CompositeItemWriter.write(CompositeItemWriter.java:51)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
      at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy28.write(Unknown Source)
      at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175)
      at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)
      at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor$3.doWithRetry(FaultTolerantChunkProcessor.java:329)
      at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:263)
      at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:193)
      at org.springframework.batch.core.step.item.BatchRetryTemplate.execute(BatchRetryTemplate.java:217)
      at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor.write(FaultTolerantChunkProcessor.java:422)
      at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199)
      at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
      at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
      at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
      at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
      at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
      at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
      at org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate$ExecutingRunnable.run(TaskExecutorRepeatTemplate.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>I have required a PROCESS permission to check SHOW ENGINE STATUS to get mor info about the deadlock.</p>
  
  <p>So, my questions are theses:</p>
  
  <ul>
  <li>The log in ...CustomerDAOImpl.findCustomer is a key to get the query and the lock table. But, is the deadlock produced because of this query/table or another one?</li>
  <li>The transaction with Atomikos is among two databases. Now, I notice that all related tables are in InnoDB engine, except one, which is in MyISAM. Is this right? Could it be the cause of this exception?</li>
  <li>Could the deadlock happen because of the number of threads?</li>
  </ul>
  
  <p>Of course, any help to check and analyze this exception is very appreciated.</p>
  
  <p>Thanks in advance</p>
  
  <p>[SOLVED EDIT]
  Rohit's advices are good ones. My next actions are:</p>
  
  <ul>
  <li>Get more info about deadlock by enabling SHOW ENGINE STATUS and getting the log.</li>
  <li>My transaction contains some SELECT before UPDATEs. I'll extract the queries out of the transaction, so it will contain only INSERT and UPDATE sentences.</li>
  </ul>
  
  <p>Thanks a lot.</p>