管理対象Beanから呼び出され、ビュー(エンド・ユーザー)のエラーを報告する汎用メソッドを構築しようとすると、問題が発生します。SQLExceptionにキャストされるEJBException
このmanagedBeanコード:
public void processEntity() {
try {
//code save, update, delete or any process here!!!
} catch (Exception e) {
errornotifier.notification("message for user", e);
}
}
これは私のerrornotifierコードです:
public static void notification(String msj, Exception exep) {
String sqlMsj = null;
try {
if (exep instanceof EJBException) {
EJBException ejbException = (EJBException) exep;
if (ejbException.getCausedByException().getCause() instanceof PersistenceException) {
PersistenceException persistExep = (PersistenceException) ejbException.getCausedByException().getCause();
Throwable throwable = persistExep;
if (throwable instanceof SQLException) {
SQLException sqlex = (SQLException) throwable;
sqlMsj = ""; //-->validatios and set message agree sqlcode ...
}
}
}
} catch (Exception ex) {
msj = msj + ex.getMessage();
}finally {
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO, "Executed", msj));
logger.error("notification failed", msj);
}
}
前のコードは、GlassFishの上で正しく動作します - ペーシュ・カショーロサーバが、エラーがで返さ以来のWebLogicでは動作しません。このサーバーはEJBExceptionのTransactionRolledbackLocalExceptionのインスタンスを参照し、SQLExceptionへのPersistenceExceptionの場合と同じようにキャストできません。
私はjpa-hibernateでデータの永続性を働かせています。私はJavaで数カ月間プログラミングしていましたので、もし私が間違っていると私が許してくれることを願っています。 。
達成しようとしているものは何ですか? 'try'ブロック内のコードは無効です – ytg
tryブロックでは、データベース内の永続性(挿入、更新、削除など)または必要なものに応じたjava文を実行できます。コードは正しく動作しますサーバーglassfishとpayaraではなく、weblogicで。 – fjrincon0
私は理解していますが、Glassfishで実際に動作しているステートメントによっては、あなたの質問に対する答えが異なる場合があります。 – ytg