Nar*_*rma 7 java spring jpa exception-handling exception
我们在我们的应用程序中使用JSF,Spring和JPA.我们正在努力简化我们项目的异常处理策略.
我们的应用程序架构如下:
UI(JSF) - >托管豆 - >服务 - > DAO
我们正在为DAO层使用Exception Translation bean后处理器.这是在Spring Application Context文件中配置的.
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
Run Code Online (Sandbox Code Playgroud)
Spring将所有数据库异常包装到'org.springframework.dao.DataAccessException'中.我们没有在DAO 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
}
}
Run Code Online (Sandbox Code Playgroud)
服务层
@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
}
}
}
Run Code Online (Sandbox Code Playgroud)
DAO层
@Repository("dao")
Class DAO{
public daoMethod(){
// No exception is handled
// If any DataAccessException or RuntimeException is occurred this
// is thrown to ServiceLayer
}
}
Run Code Online (Sandbox Code Playgroud)
问题: 我们只想确认上述方法是否符合最佳实践.如果没有,请告诉我们处理异常的最佳方法(玩交易管理)?
| 归档时间: |
|
| 查看次数: |
6615 次 |
| 最近记录: |