Announcement Announcement Module
Collapse
No announcement yet.
Newbe dao question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbe dao question

    Hi

    All my Dao methods throws DataAccesException.
    Should my bussiness methods (which are only call these dao methods) throw DataAccessException too?
    What is the preffered way to handle exceptions came from the dao's? (and go to the view tier)

    thanks

  • #2
    Should my bussiness methods (which are only call these dao methods) throw DataAccessException too?
    DataAccessException is a runtime exception so you don't need to declare it to be thrown anywhere.

    What is the preffered way to handle exceptions came from the dao's? (and go to the view tier)
    Take a look at the sample petclinic-servlet.xml file, specifically the bean with id "exceptionResolver". This may do what you want.

    Comment


    • #3
      Services may catch DataAccessException and throw application specific exceptions (UserDisabledException, AccountLockedException... ).
      You can also, just rethrow DataAccessException and let the upper layer (View Tier) handle it.
      HTH

      Comment


      • #4
        "DataAccessException is a runtime exception so you don't need to declare it to be thrown anywhere."

        does it mean that i don't have to declare it on dao methods either?
        In that case, what is the mechanism, that bind technology specific exceptions(HibernateException, JDOException etc. thrown by dao methods)
        to DataAccessException?
        In the view tier, i'd like to catch only DataAccessException for this methods (if necessery)

        thanks

        Comment


        • #5
          DataAccessException never needs to appear on any of your method signatures. It's an unchecked exception after all.

          You can safely catch the DataAccessException (or its subclasses) in any of your tiers: persistence, services, web. Or just leave it to propagate to a web exception resolver or the servlet container. Most of the time I tend to catch them in the services layer, returning nulls or more often meaningful business exceptions. Especially for DataRetrievalFailureException.

          Spring's support classes automatically catch checked exceptions (like thrown by Hibernate) and convert them into an unchecked DataAccessException. This not only relieves you from having to catch checked persistence exceptions, but it gives you consistency between persistence frameworks that yout DAO might use.

          Comment


          • #6
            thanks for reply

            "DataAccessException never needs to appear on any of your method signatures"

            If that's the case, why petstore and petclinic samples do that in their DAO's?

            Comment


            • #7
              If that's the case, why petstore and petclinic samples do that in their DAO's?
              This is often done as a kind of self documentation to inform potential callers of what can be thrown.

              Comment

              Working...
              X