Wednesday, March 26, 2008

Java SQLException Chaining

In JDK 1.4 chained Throwables were added and made logging and tracing exceptions much easier. Being able to create a new exception and specify the causing exception in a standard way, tied to JVM printing out the stack chain, was a big boost to tracking down problems in an application.

There are still some exceptions that do not use this standard chaining mechanism and one of the most frustraiting cases in SQLException. It provides a getNextException() method which returns the next SQLException related to the failed operation. The problem here is without special handling code these nextExceptions just get lost in log messages.

A general solution to this problem is to add an Aspect that either logs the results of getNextException or adds them to the exception chain.



This aspect class can be used via Spring's AOP support with the following context configuration:

No comments: