アプリケーションでは JSF、Spring、および JPA を使用しています。プロジェクトの例外処理戦略を簡素化しようとしています。
私たちのアプリケーション アーキテクチャは以下のようなものです。
UI(JSF) --> Managed Beans --> サービス --> DAO
DAO レイヤーに Exception Translation bean ポスト プロセッサを使用しています。これは、Spring Application Context ファイルで構成されます。
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
Spring はすべてのデータベース例外を「org.springframework.dao.DataAccessException」にラップします。DAO レイヤーでは、その他の例外処理は行っていません。
以下のような例外を処理するための戦略:
プレゼンテーション層:
Class PresentationManangedBean{
try{
serviceMethod();
}catch(BusinessException be){
// Mapping exception messages to show on UI
}
catch(Exception e){
// Mapping exception messages to show on UI
}
}
サービス層
@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レイヤー
@Repository("dao")
Class DAO{
public daoMethod(){
// No exception is handled
// If any DataAccessException or RuntimeException is occurred this
// is thrown to ServiceLayer
}
}
質問: 上記のアプローチがベスト プラクティスに従っているかどうかを確認したいだけです。そうでない場合は、例外を処理する最善の方法を提案してください (トランザクション管理で遊んでください)。