title: Exception Handling Strategy with Spring/JPA/JSF
body: <p>We are using JSF, Spring and JPA in our application. We are trying to simplify our Exception Handling strategy for our project.</p>
  
  <p>Our application architecture is like below: </p>
  
  <p>UI(JSF) --> Managed Beans --> Service --> DAO</p>
  
  <p>We are using Exception Translation bean post processor for DAO layer. This is configured in Spring Application Context file.</p>
  
  <pre><code>&lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt; 
  </code></pre>
  
  <p>Where Spring wraps all database exceptions to 'org.springframework.dao.DataAccessException'. We are not doing any other exception handling in DAO Layer.</p>
  
  <p>Our strategy to handle exceptions like below:</p>
  
  <p><strong>Presentation Layer:</strong></p>
  
  <pre><code>Class PresentationManangedBean{
  
   try{
        serviceMethod();
     }catch(BusinessException be){
        // Mapping exception messages to show on UI
     }
     catch(Exception e){
         // Mapping exception messages to show on UI
     }
  
  }
  </code></pre>
  
  <p><strong>Service Layer</strong></p>
  
  <pre><code>@Component("service")
  Class Service{
  
   @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = BusinessException.class)
   public serviceMethod(){
  
    try{
  
        daoMethod();
  
     }catch(DataAccessException cdae){
        throws new BusinessException(); // Our Business/Custom exception
     }
     catch(Exception e){
        throws new BusinessException();  //  Our Business/Custom exception
     }
   }
  
  }
  </code></pre>
  
  <p><strong>DAO Layer</strong></p>
  
  <pre><code>@Repository("dao")
  Class DAO{
  
   public daoMethod(){
    // No exception is handled
    // If any DataAccessException or RuntimeException is occurred this 
    // is thrown to ServiceLayer
   }
  
  }
  </code></pre>
  
  <p><strong>Question:</strong>
  We just want to confirm whether above approach is as per the best practices. If not, please suggest us the best way to handle the exceptions (playing with transaction management)?</p>