0

I have a problem trying to build a generic method that is called from the managed bean and report the errors in the view (end user).

this a managedBean code:

 public void processEntity() {
    try {
        //code save, update, delete or any process here!!! 
    } catch (Exception e) {
        errornotifier.notification("message for user", e);
    }
}

this is my errornotifier code:

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);
    }
}

The previous code works correctly on glassfish - payara servers but does not work in weblogic since the error returned on this server refers to the instance of the TransactionRolledbackLocalException of the EJBException and I can not cast the same as I did for the PersistenceException to SQLException.

I work the persistence of data with jpa - hibernate, I have only been programming for a few months in java so I hope you forgive me if I do it wrong, if there is a better way to do it please tell me.

Dagon
  • 145
  • 2
  • 12
  • what exactly are you trying to achieve? your code in the `try` block has no effect – ytg Oct 31 '17 at 07:40
  • In the try block you can execute any process either persistence in database (insert, update, delete, etc.) or java statements according to what is required, I repeat the code works correctly in servers glassfish and payara but not in weblogic. – Dagon Oct 31 '17 at 19:12
  • I understand, but depending on what statements actually work in Glassfish, the answer to your question could be different. – ytg Nov 02 '17 at 13:59

1 Answers1

0

You're right; since the proprietary layers on top of the actual JDBC driver are hidden from you when you use an application server's JDBC pooling, you should not be hardcoding a dependency on how exceptions are wrapped.

Assuming you are just trying to find the underlying SQLException, if any, you can work your way up the exception chain. Use an approach like this one.

public static SQLException getSQLException(Exception exep) {
    while (exep != null && !(exep instanceof SQLException)) {
        exep = exep.getCause();
    }
    return exep;
}
Yosef Weiner
  • 5,432
  • 1
  • 24
  • 37
  • Hello, this test is already done and is satisfactory for the glassfish server, however in weblogic the sql errors are returned from the TransactionRolledbackLocalException code: ` while (exep != null && !(exep instanceof SQLException)) { exep = exep.getCause(); String msj = exep .getClass().getCanonicalName(); //msj --> weblogic.transaction.internal.AppSetRollbackOnlyException } ` This Log server : how to cast SqlExceptionHelper to SQLException – Dagon Oct 31 '17 at 19:27