In Java 7 is it possible pass the method as a paramater.
The method would be invoked and would encapsulated in a do while to check for sql deadlocks and retry.
I am trying to make a generic way of handling deadlocks when there is a lock on the table, as there is currently thousands of inserts and updates to replicate this across. At present it would only look for SQLException but this could be changed to getErrorCode of SQLException and target ER_LOCK_DEADLOCK.
The code below is just an example of what I am trying to achieve:
public void Object1Insert(Object1 object1){
genericSQLRetry(insertObject1(object1));
}
public void Object2Insert(Object2 object2){
genericSQLRetry(insertObject2(object2));
}
public void genericSQLRetry({method}){
int retries = 5;
boolean isException = false;
do{
try {
isException = false;
retries--;
//Invoke method
{method}
}catch (SQLException e){
isException = true;
}
if (isException & retries > 0){
//getRandomSleep would be between 750-1250ms. This is to stop multiple retries at the same time.
Thread.sleep(getRandomSleep());
}
}while (isException == true && retries > 0);
}