tags:

views:

143

answers:

2

I have a Java application which queries a database table which the current user account may or may not have access to. If an exception is returned, I would like to distinguish between AccessDenied and the database server being down/offline. I expected that db2 would throw a PermissionDeniedDataAccessException when the user does not have select privileges on a table. However, when I tested it, db2 threw an InvalidDataAccessResourceUsageException.

Is it safe to catch an InvalidDataAccessResourceUsageException and handle it as an AccessDenied exception? Why doesn't db2 throw a PermissionDeniedDataAccessException?

FYI: I am using the JDBC driver for db2 and not the ODBC driver so I expected better Java support.

A: 

Interestingly, you have tagged this question with 'spring', although you don't mention it any further. You should consider using Spring JdbcTemplate for accessing the DB. As a result, all exceptions thrown will be translated into generic Spring exceptions, and you don't need to concern yourself with the specific DBMS Exceptions. It will translate any exception into (a subclass of) DataAccessException. There is also PermissionDeniedDataAccessException, which may occur if the user does not have permission.

EJB
Actually, I am already using the Spring integration with ibatis and it doesn't seem to be handling this case correctly. That's why I was expecting to receive a PermissionDeniedDataAccessException instead of an InvalidDataAccessResourceUsageException (which inherits from DataAccessException).
David
A: 

If Spring exception translation algorithm incorrectly maps SQL exception code to the Spring's exception, you can determine the SQL code yourself and customize exception translation, as described here: 12.2.4 SQLExceptionTranslator

axtavt
I actually didn't know that I can make my own exception trapper in Spring. This will let me do by trapping specific sqlcodes.
David