Here's the logic I used:
int retries = config.get("retries");
Response resp = null
do {
try {
resp = operation.execute();
retries = 0;
} catch (Exception ex) { //Note. Please excuse this catch pattern. Its not a problem now.
if isAParticularException(ex) { //call a method to check the wrapped exception and other details
retries--;
LOGGER.info("Integrity exception, can be retried");
if (retries == 0) {
LOGGER.info("Retry exhausted");
throw ex;
}
LOGGER.info("Retrying operation, retry count " + ledgerRetry);
} else {
throw ex;
}
}
} while (retries > 0);
return resp;
Number of retries is considering original operation as well. But the problem is that
- if I return from try block directly without assigning anything, then SCA (Fortify for me) reports that the variable retries is not read (in success flow), and
- if I assign and do as above, then SCA shouts about the immediate reassignment of value to the retries variable without even reading it.
Considerations:
- The first call should be independent of whatever value we read for 'retries'
- Duplicate code should be avoided, and avoiding recursion will be nice too.
May be a simple thing, but I am not catching it probably. Please suggest.