Announcement Announcement Module
Collapse
No announcement yet.
How should I process Exception in DAO? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How should I process Exception in DAO?

    Hi friends:

    I want to know how to process Exception in DAO layer. For example.there are two method to do this:
    one method:


    Code:
    public class PersonDAOImpl implements PersonDAO{ 
        
           public void updatePerson(Person p){ 
                 ........................................... 
                 try{ 
                           session.update(p); 
                           tx.commit(); 
                      }catch(HibernateException ex){ 
                              tx.rollback(); 
                              ex.printStackTrace(); 
                        }finally{ 
                                session.close(); 
                          } 
                      ....................................

    Two method:
    Code:
    public void updatePerson(Person p){ 
                 ........................................... 
                 try{ 
                           session.update(p); 
                           tx.commit(); 
                      }catch(HibernateException ex){ 
                             throw new MyException("HibernateEx"); 
                        }
    If I use the second, I can catch the "MyException" in Servlet and sendRedirect to the specific error page.But I can't process it finally for example rollback transaction or close session.
    If I use the first one,I can't catch the Exception in Servlet ,So I can't send redirect to the specific error page.
    Which is better ? Shoud I process exception in DAO Layer or web layer? Thks!
    [/code]

  • #2
    Why not let Spring manage your Hibernate session and transaction - see samples.

    If you need a completly separate page to display the error, try using an exception resolver (also in samples):
    Code:
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
       <property name="exceptionMappings">
          <props>
             <prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
             <prop key="org.yourcompany.DatabaseErrorException ">dataAccessFailure</prop>
          </props>
       </property>
    </bean>
    Code:
    dataAccessFailure.class=org.springframework.web.servlet.view.JstlView
    dataAccessFailure.url=/WEB-INF/jsp/dataAccessFailure.jsp

    Comment


    • #3


      Thks for your reply.
      Because I throws DataAccessException in all DAO class method. So I couldn't catch specific exception in my Servlet and proccess them in different way. DataAccessException is a RuntimeException,so I could't catch it in Servlet. How to send Redirect to different error page according different condition?

      Comment


      • #4
        Re:

        Originally posted by liren
        Thks for your reply.
        Because I throws DataAccessException in all DAO class method. So I couldn't catch specific exception in my Servlet and proccess them in different way. DataAccessException is a RuntimeException,so I could't catch it in Servlet. How to send Redirect to different error page according different condition?

        you can use org.springframework.orm.hibernate.SessionFactoryUt il

        to convert to DataAccessException

        Comment


        • #5
          Why not catch the DataAccessException subclasses you can recover or retry in the DAO layer? Or business layer? DataAccessException provides an informative hierarchy to facilitate this.

          Comment


          • #6


            Originally posted by Rod Johnson
            Why not catch the DataAccessException subclasses you can recover or retry in the DAO layer? Or business layer? DataAccessException provides an informative hierarchy to facilitate this.
            Now,I write a class that extends DataAccessException. For example:
            Code:
             public class MyDataAccessException extends DataAccessException&#123;
             ................................. 
              &#125;
             public class YourDataAccessException extends DataAccessException&#123;
             ................................. 
              &#125;
            In servlet , I can do it in this way:
            Code:
             public class myServlet extends HttpServlet&#123;
              public void doPost&#40;.......................&#41;....&#123;
                    try&#123;
                            mydao.getList&#40;&#41;;
                        &#125;catch&#40;MyDataAccessException myex&#41;&#123;
                               //do something
                        &#125;catch&#40;YourDataAccessException yourex&#41;&#123;
                               //do something
                        &#125;
            Or in this way:
            Code:
            public void doPost&#40;.......................&#41;....&#123;
                    try&#123;
                            mydao.getList&#40;&#41;;
                        &#125;catch&#40;Exception  myex&#41;&#123;
                          if&#40;myex instanceof MyDataAccessException&#41;&#123;
                               //do something
                            &#125;
                        &#125;catch&#40;YourDataAccessException yourex&#41;&#123;
                           if&#40;yourex instanceof YourDataAccessException&#41;&#123;
                               //do something
                            &#125;
                        &#125;
            That what you mean? It is right method to do this? I mean that I could catch DataAccessException thought it is runtimeException?

            Comment


            • #7
              Among DataAccessException subclasses are DataIntegrityViolationException, CannotAcquireLockException, CannotSerializeTransactionException, TypeMismatchDataAccessException... I think all classes/exception in org.springframework.dao package.
              Basically, you can either catch one of those exceptions, or, as you did in your code, create your own application exception if no equivalent exception is provided by Spring.
              HTH

              Comment

              Working...
              X