Search code examples
javamysqljdbctransactionsdatabase-deadlocks

How to restart transaction after deadlocked or timed out in Java ?


How to restart a transaction (so that it executes at least once) when we get:

( com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException:Deadlock found when trying to get lock; Try restarting transaction ) OR ( transaction times out ) ?

I'm using MySQL(innoDB ENGINE) and Java.Please help and also link any useful resources or codes.


Solution

  • When ever you are catching such type of exception in your catch block

    catch(Exception e){
    if(e instanceof TransactionRollbackException){
     //Retrigger Your Transaction
       }
    // log your exception or throw it its upto ur implementation
    }