Wednesday, March 22, 2006

Mustang Promises a Better SQLException

Java6, codenamed Mustang, has some real niceties in SQLException implementation. Some of these were real problems in writing JDBC applications - it is a welcome move to have these enhanced in the upcoming JDBC 4.0. Lance Andersen, the spec lead for JDBC 4.0, has all the details in his blog, along with other enhancements.

Some of the very useful ones in SQLException are :

Real Chaining Support

SQLException now supports additional constructors with the "cause" specified as argument. The causal relationship can now be used extensively to program chaining of exceptions. Here is a snipped adopted from Lance.

catch(SQLException ex) {
while(ex != null) {
   Throwable t = ex.getCause();
   while(t != null) {
      System.out.println("Cause:" + t);
      t = t.getCause();
   }
   ex = ex.getNextException();
}
}


Categorization


Two distinct categories of SQLExceptions will be supported - SQLTransientException and SQLNonTransientException. SQLNonTransientException is more of a runtime exception (would have loved to see it derived from RuntimeException though) where retry will not help, unless the problem is explicitly rectified. The classes included as SQLNonTransientException are

  • SQLFeatureNotSupportedException

  • SQLNonTransientConnectionException

  • SQLDataException

  • SQLIntegrityConstraintViolationException

  • SQLInvalidAuthorizationException

  • SQLSyntaxErrorException


SQLTransientException is more of a checked exception with the following subclasses

  • SQLTransientConnectionException

  • SQLTransactionRollbackException

  • SQLTimeoutException


For more details on the advances in JDBC 4.0 features refer to this artima article.

No comments: