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)?
This approach looks good to me.We are using same kind of approach in our project.
Vinay
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With