We are using JSF, Spring and JPA in our application. We are trying to simplify our Exception Handling strategy for our project.
Our application architecture is like below:
UI(JSF) --> Managed Beans --> Service --> DAO
We are using Exception Translation bean post processor for DAO layer. This is configured in Spring Application Context file.
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
Where Spring wraps all database exceptions to 'org.springframework.dao.DataAccessException'. We are not doing any other exception handling in DAO Layer.
Our strategy to handle exceptions like below:
Presentation Layer:
Class PresentationManangedBean{
try{
serviceMethod();
}catch(BusinessException be){
// Mapping exception messages to show on UI
}
catch(Exception e){
// Mapping exception messages to show on UI
}
}
Service Layer
@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
}
}
}
DAO Layer
@Repository("dao")
Class DAO{
public daoMethod(){
// No exception is handled
// If any DataAccessException or RuntimeException is occurred this
// is thrown to ServiceLayer
}
}
Question: 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)?