tags:

views:

65

answers:

5

Hello,

I have a piece of code that may or may not fail, which I want to try X number of times before giving up. When it fails, it throws a specific Exception, so I figured something like this would work:

    int retries = 0;
    while (retries < MAX_RETRIES) {
        failedFlag = false;
        try {
            //...do some stuff...
            logger.info("After commit...");
            if (!failedFlag) {
                logger.info("Failed flag is false, so breaking out.");
                break;
            }
        } catch (MyException e) {
            retries++;
            failedFlag = true;
            long sleepMillis = MILLIS_TO_SLEEP * retries;
            logger.warn("Caught a failure");
            logger.warn("Will sleep for " + sleepMillis + "msec and then try again.");
            try {
                Thread.sleep(sleepMillis);
                logger.info("Done sleeping...");
                logger.info("Failed flag is " + (failedFlag ? "true" : "false"));
            } catch (InterruptedException e1) {
                logger.warn("Caught interrupted exception while sleeping.  Terminate.");
                transaction.rollback();
                return;
            }
        }
    }

The problem I'm having is that the change made to failedFlag in the first catch block doesn't seem to persist. Upon failure, the thread sleeps, but when it wakes up and re-enters the try block, the failedFlag reverts back to false? I get the following log lines:

2010-09-16 17:09:48,448 WARN  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Will sleep for 60000msec and then try again.
...
2010-09-16 17:10:48,449 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Done sleeping...
2010-09-16 17:10:48,450 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Failed flag is true
2010-09-16 17:10:48,453 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - After commit...
2010-09-16 17:10:48,453 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Failed flag is false, so breaking out.

Kinda boggled here... Are local variables unaffected by changes made in catch blocks?

+2  A: 

If you put the failed flag above the while loop, it will have a scope outside the loop. Currently, with the scope you have, the flag is redefined every time the loop restarts.

Hope that helps.


Edit: Just to be more clear, you should change

int retries = 0;
while (retries < MAX_RETRIES) {
    failedFlag = false;

to

int retries = 0;
failedFlag = false;
while (retries < MAX_RETRIES) {
aperkins
My bad.. please ignore.
Ashish Patil
No, the output is exactly what it should have been. If he moves the failed flag above the while loop, i.e. declares it false before the loops starts, then it will not always be false when the loop starts. If he moves that line above the while loop, it will be true when the loop starts after failing (catching the exception).
aperkins
@Ashish: no problem, We all misread stuff all the time :D Thanks for the double check though - helps keep me honest ;)
aperkins
+3  A: 

You are setting failedFlag to false at the top of the while loop.

letronje
A: 

The first thing you do in the while loop is set the flag to false. So when an exception occurs, you set the flag to true, sleep for a bit, then set the flag to false and retry. Unsuprisingly, after the retry its still false...

Chris Dodd
A: 

You're setting failedFlag every time you start the loop, so it will always have the false value when you enter the try block. You probably want to initialize it outside of the while loop.

zigdon
A: 

Yes local variables are affected by changes in catch blocks.

And obviously failedFlag goes back to false (at the next iteration), as it prints "Failed flag is false, so breaking out."

Colin Hebert