title: How to properly handle InnoDB deadlocks in Java/JDBC?
body: <p>I am working on a theory basis here, I want to make sure all my bases are covered.</p>
  
  <p>I have read quite a bit into InnoDB with Java and how deadlocks can occur no matter what query you are running. Although I am pretty clued in with the theory and best practices, I am pretty much clueless on how to implement the catch all mechanism of reissuing transactions when a deadlock occurs.</p>
  
  <p>Are there specific exceptions to listen out for? Is the exception only thrown after I call <code>connection.commit()</code> or can it occur as soon as I execute a <code>PreparedStatement</code>? Should things be running in a loop with a limit to how many times the loop runs?</p>
  
  <p>I essentially just need a bare bones Java code example of how this thing is generally handled. As I am not sure where things factor in such as, do I re-instantiate <code>PreparedStatement</code> objects or close them first etc etc, it's all very confusing. Same goes for <code>ResultSet</code> objects too.</p>
  
  <p><strong>Edit:</strong> I should probably mention that I am working with transactions, setting auto commit to 0 etc.</p>
  
  <p><strong>Edit 2:</strong> Am I on the right track with this pseudo code? I have no clue</p>
  
  <pre><code>do
  {
      deadlock = false
  
      try
      {
          // auto commit = 0
          // select query
          // update query
          // delete query
          // commit transaction
      }
      catch (DeadLockSpecificException e)
      {
          deadlock = true
      }
      finally
      {
          // close resources? statement.close(), resultset.close() etc?
          // or do I reuse them somehow and close them after the do/while loop?
          // this stuff confuses me a lot too
      }
  }
  while (deadlock == true);
  </code></pre>