I am working on a theory basis here, I want to make sure all my bases are covered.
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.
Are there specific exceptions to listen out for? Is the exception only thrown after I call connection.commit()
or can it occur as soon as I execute a PreparedStatement
? Should things be running in a loop with a limit to how many times the loop runs?
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 PreparedStatement
objects or close them first etc etc, it's all very confusing. Same goes for ResultSet
objects too.
Edit: I should probably mention that I am working with transactions, setting auto commit to 0 etc.
Edit 2: Am I on the right track with this pseudo code? I have no clue
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);
Is the exception only thrown after I call connection.commit()?
, I meant specifically for the exception thrown when a deadlock occurs. Can a deadlock error only happen once I've calledconnection.commit()
or can it happen the second I execute an update or query, even though I have autocommit disabled? – Kempe