Announcement Announcement Module
Collapse
No announcement yet.
Ignore certain Exceptions when using Spring JdbcTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ignore certain Exceptions when using Spring JdbcTemplate

    Hi,

    I am using Spring JdbcTemplate to run my queries.

    I ran into some trouble when trying to control the exception thrown by the jdbcTemplate.
    With regards to DAO, spring using its own hierarchy of exceptions.
    The exceptions thrown are of type DataAccessException.

    I want to be able to ignore certain exceptions, for example I run a query which give SQLException, with error code 11111. , I could check the error code and act accordingly.

    The DataAccessException doesn't expose such method.

    So what I did is to use the getCause method of the exception to investigate the exception (I don't like this solution), Until I get to the SQLException, and take the error code from it.

    I tried to extends from SQLErrorCodeSQLExceptionTranslator, and when get a specific error, return null, but it didn't help. the Spring have several mechanisms to handle exceptions after trying to use the custom translators.

    Code:
    public class AdgSQLErrorCodesTranslator extends SQLErrorCodeSQLExceptionTranslator {
    	
    	protected DataAccessException customTranslate(String task, String sql, SQLException sqlex) {
            if (sqlex.getErrorCode() == 12920) {
            	//Do nothing, ignoring error: database is already in force logging mode
            }
            return null;
        }
    }
    Have any idea??

    Thanks!!

  • #2
    Spring already does conversion based on the error code/type of exception. It is an exception and as such spring will rollback the transaction, if there would be no exception that would break springs tx support.

    The easiests would be to extend the support, create your own subclass of DataAccessException and convert to that exception. In your code you could then configure spring tx support to ignore that exception.

    Comment


    • #3
      Thanks Marten for the answer.

      So to sum it up, I will need to extend from DataAccessException and create something like : DataAccessIgnoredException

      How do I configure spring tx support to ignore this exception?
      Is this a good way to solve this issue?
      Creating an exception just to ignore it, doesn't sound good.

      Thanks,
      Tomer.

      Comment


      • #4
        Catching it, extracting the root, check the error code if not 11111 then rethrow else swallow isn't either nor reusable (well you could put it in an around aspect and make sure it runs after the tx advice), when you extend the framework in this way it is reusable. .

        I suggest the reference guide you can specify which exceptions to ignore/not rollback for. It depends on if you use annotations, xml or the old TransactionalProxyFactoryBean.

        Comment


        • #5
          Amm, I think we are not talking on the same thing.
          I am neither using transactionalManager , nor AOP.

          I have a class, that warps the jdbcTemplate, and more or less doing the same.
          This class job is to execute queries, it doesn't have any thing involved business logic.

          The thing is, when using it for example:
          Code:
          Sting command = "alter database ...";
          try {
          	conn.execute(command);
          } catch (HADataAccessException e) {
          	logger.error("Failed to run: "+command,e);
          	return false;
          }
          I wish to be able to ignore certain exceptions.
          For example I expect to get an exception of error code 11111, while running "command" .

          So inside the catch clause I will have to investigate the HADataAccessException , and check its error code.
          Instead I can extend SQLErrorCodeSQLExceptionTranslator, to translate the 11111 excpetion into a certain DataAccessException , for example DataAccessIgnoredExcpetion. and then (This part I don't know ) I need to tell the spring to ignore DataAccessIgnoredExcpetion exceptions.


          Maybe I am all wrong, but please correct me..

          Thanks!

          Comment


          • #6
            That isn't going to work... You can only tell spring to ignore it if you use springs transactionmanagement without it that isn't possible and you will have to write your own.

            Also I really hope you are using transactions (as you aren't using a transactionmanager) and if you are programming your own transactions I strongly suggest you change to declarative transactions.

            Comment

            Working...
            X