ravi
ravi

Reputation: 838

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.

Upvotes: 4

Views: 4173

Answers (2)

BOSS
BOSS

Reputation: 2951

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
}

Upvotes: 1

Op De Cirkel
Op De Cirkel

Reputation: 29473

If you use plain JDBC, you have to do it manually, in a loop (and don't forget to check the pre-conditions every time.
If you use spring, "How to restart transactions on deadlock/lock-timeout in Spring?" sould help.

Upvotes: 1

Related Questions