1
votes

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

1
You could wrap in separate try-catch blocks the instruction that connects to the DB and the ones that execute queriesRaul Rene
Actually database connection code is written in many of the places in application but Log exception code is written at only one place, so it will be better to handle it from Log codeagarwal_achhnera
Then you've done it wrong already. Your code architecture is poor. Fix it.user207421

1 Answers

0
votes

you can use error code and check it for failed condition like below

Exception e;
int errorCode = e.getErrorCode();
if(errorCode==4005)
{
//do whatever you want to do for failed connection
}