Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing a small database-centric application in Java Swing that will be used internally in a small company. I haven't much experience with developing production-level applications.

I don't use an ORM, just a few simple methods that run SQL queries with Java JDBC. On every method that access the database I use a try-catch statement for SQLException and IOException. Should I do any logging here? In case I should, how much should I log? Please give me examples of good practice in this case.

share|improve this question
    
This is more a question for SO isn't ? –  user2567 Sep 27 '10 at 10:37
1  
@Pierre: I thought it was subjective and argumentative? It doesn't have an exact answer? It's more of a discussion? –  Jonas Sep 27 '10 at 10:43
    
hmmm I don't know. The answer is pretty obvious. I can answer if you want ? –  user2567 Sep 27 '10 at 10:46
2  
@Pierre: You start your answer with "IMHO", so you subconsciously think the question is subjective. –  bigown Sep 27 '10 at 15:50
2  
+1 for that bigown –  user2567 Sep 27 '10 at 16:18
add comment

2 Answers

up vote 5 down vote accepted

Two main things in production system are:

  1. Don't blow up the logs with information that is not interesting
  2. Allow raising the log level for troubleshooting purpose.

Use some kind of a logging infrastructure. If the exception can occur during the normal operation, report it in debug level. If it is a real problem, report it in error level. Either way you will have a way to troubleshoot the system by raising the log level.

Also - when you report an exception always make sure the the entire stack trace + inner exceptions are printed.

share|improve this answer
add comment

IMHO, here is the very minimum you should do. I assume you are using Log4J.

try
{
   // open connection

   // begin transaction

   // my database stuff

   // commit transaction
}
catch (SQLException ex)
{
   log.Error("An error occured while ...: " + ex.Message, ex);
   // do stuff related to SQLException

   // rollback transaction
}
catch (IOExcpetion ex)
{
   log.Error("An error occured while ...: " + ex.Message, ex);
   // do stuff related to IOException

   // rollback transaction
}
catch (Exception ex)
{
    log.Error("An error occured while ...: " + ex.Message, ex);
    // manage this unhandled exception

    // rollback transaction
}
finally
{
   // close connection
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.