Proper handling of database-related exceptions

by Dejan Bosanac

This is the issue that I keep running into on various projects, so even if it seems obvious I think it is worth spending a post on it.

Some OR mapping frameworks tend to use batch update operations when dealing with the database. The "problem" that this approach introduces is that if any problem occurs during the update, a java.sql.BatchUpdateException exception will be thrown instead of the regular java.sql.SQLException. If we don't handle BatchUpdateException properly the message it will give us will not be very useful.

3 Comments

Brian Ewins
2006-03-10 07:35:37
It's interesting that you use 'logger.info(exception);' to log the exception, and then explicitly loop over the causes. Is this an explicit design choice, over 'logger.info(message, exception)' ? (the latter prints the stacktrace including the cause)
Dejan Bosanac
2006-03-13 03:58:10
People use various logging APIs so I wanted to show the most generic solution here. It serves demonstration purposes only.


Thanks.

Paul Saladna
2006-03-20 22:47:41
You can get the same functionality by using the Commons Lang class/method ExceptionUtils.getFullStackTrace(Throwable t).


http://jakarta.apache.org/commons/lang/api-release/index.html