In my java application, log file have large number of SQLException traces due to database connection failure. which try after each 5 seconds. So I have to know that SQLException goes to log is either due to database connection failure or due to some miss happening in sql query. So that if database connection failure occure in between and try to log more then three or 4 time then I can limit this to write after 1 minute.Log method is as below
public static synchronized void log( Exception e, boolean forcePrint) { if(sqlExceptionLogTime!=null && e instanceof SQLException &&((new Date()).getTime()-sqlExceptionLogTime.getTime())<60000){ // do nothing }else if ( (forcePrint || debugFlag) && (logStream != null)) { e.printStackTrace(logStream); logStream.flush(); logcount++; if(e instanceof SQLException){ sqlExceptionLogTime = new Date(); } } }
In this sqlExceptionLogTime is a static date type variable
try-catch
blocks the instruction that connects to the DB and the ones that execute queries – Raul Rene