Announcement Announcement Module
Collapse
No announcement yet.
Best practices for handling DataAccessException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best practices for handling DataAccessException

    Hi,
    I have successfully tested a streamlined DAO, which I am now unsuccessfully trying to break to test exception handling. This is based on Juergen's doc comments in SqlMapClientTemplate. I am confused on how best to handle these unchecked exceptions:

    new method:
    Code:
    public void deleteFoo(Foo foo) throws DataAccessException {
            getSqlMapClientTemplate().delete("fooAlias.fooDeleteMap", args);
    }
    I found something by Rod Johnson, an early ServerSide post

    Code:
    try {
    	// do work
    }
    catch (OptimisticLockingFailureException ex) {
    	// I'm interested in this
    }
    catch (DataAccessException ex) {
    	// Fatal; just rethrow it
    }
    Juergen's comments in SqlMapClientTemplate suggest that my DAO's do not need to catch exceptions using the first pattern above - making the DAO's so streamlined. Yet, when thrown, what are best practices or is there any guideline for handling those exceptions? Some implementation strategy? We have our homegrown Exception class, do I need to still have
    Code:
    } catch (DataAccessException e) {
      throw MyAppException...
    }
    thanks!
    Last edited by Helena; Feb 4th, 2008, 12:45 PM.

  • #2
    It depends where you want to do it. Putting the try/catch in your dao isn't going to do much, the transaction is committed after method execution (which means you are already out of the method). The only place to put that is in the the method calling the transactional method. However even better is to write a interceptor/aop advice which converts the DataAccessExceptions into your own Exceptions.

    Comment


    • #3
      AOP to convert DataAccessException to MyException

      Originally posted by mdeinum View Post
      However even better is to write a interceptor/aop advice which converts the DataAccessExceptions into your own Exceptions.
      I like that idea, but I've not yet seen an example of how to do that with spring AOP. Does anyone have a code snippet example to share using <aop:config/>?

      Can Spring AOP handle this? I won't start implementing aspectJ for another month.

      thank you.

      Comment


      • #4
        Why not... There are plenty of samples around on the web...

        Comment


        • #5
          ...I just haven't seen an example of transforming one exception to another. maybe i misunderstood you. perhaps you meant an after-throwing (one exception) throw another, but I'll look for examples.

          Comment


          • #6
            That's the one I meant.

            Comment

            Working...
            X