Results 1 to 7 of 7

Thread: How should I process Exception in DAO?

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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

  3. #3

    Default :(

    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?

  4. #4

    Default Re: :(

    Quote 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

  5. #5
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  6. #6

    Default :)

    Quote 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?

  7. #7
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Replies: 3
    Last Post: Oct 5th, 2005, 08:39 AM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Replies: 0
    Last Post: Jul 11th, 2005, 05:49 PM
  5. Replies: 3
    Last Post: Nov 8th, 2004, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •