私たちはアプリケーションでJSF、Spring、およびJPAを使用しています。私たちは、プロジェクトの例外処理戦略を単純化しようとしています。Spring/JPA/JSFでの例外処理ストラテジ
当社のアプリケーションのアーキテクチャは、以下のようなものです:
UI(JSF) - >管理Bean - >サービス - > DAO
私たちは、DAO層のための例外翻訳ビーンポストプロセッサを使用しています。これは、Springアプリケーションコンテキストファイルで設定されます。
<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
}
}
質問: 私達はちょうど上記のアプローチは、ベストプラクティスのとおりであるかどうかを確認したいです。そうでない場合は、例外処理(トランザクション管理を行う)に最も適した方法を提案してください。
私はまた、このapprochをフォローしたいが、私は例外をキャッチし、私たちのカスタム例外をスローしたときに、私の例外ハンドラは、そのexcetionをCATHありません。 Springは私の例外を 'org.springframework.transaction.TransactionSystemException'にラップします。これは私の質問http://stackoverflow.com/questions/28295684/unable-to-wrap-dao-exception-in-service-layer-using-spring-mvcです。どうすれば問題を解決できますか? –