I am trying to have a retry logic for getting JDBC connection in case I get SQL Exception with something like :
int counter = 0;
Connection conn = null;
while (null == conn) {
try {
conn = GetConnectionObject;
} catch (SQLException e) {
if (++counter > MAX_RETRY) {
//Log ERROR
break;
}
} finally {
if (null != conn) {
try {
DbUtils.close(conn);
} catch (SQLException e) {
logger.error("Exception while closing the connection object");
}
}
}
}
I cannot test this currently hence need some help.
This will work fine if I get exception and then I can log after retrying. But if we DO NOT get exception, it will come to the finally block and close the connection. Try-Catch-Finally are inside while loop.
So If I close my connection, flow if reach
while( null== conn)
Will my connection object become null after closing ?
Or If there is some other way around to implement retry part ?
conn
tonull
will make itnull
. Closing a connection doesn't make the variable null, it just closes the connection. – Mark Rotteveel