Forum Stats

  • 3,817,369 Users
  • 2,259,322 Discussions
  • 7,893,760 Comments

Discussions

DMLException error handeling in ADF

Hi,

I have a scenario in which i have to handle the DMLException through customException handeler. The exceptionHandeler i have used is given below.

package jdbc.view;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.sql.SQLException;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import javax.servlet.http.HttpSession;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCErrorHandlerImpl;

import oracle.jbo.DMLException;
import oracle.jbo.common.JBOClass;


import javax.faces.context.FacesContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

/*
* Example sourced from Steve Muench example #129
*/
public class JdbcDCErrorHandlerImpl extends DCErrorHandlerImpl {
    public JdbcDCErrorHandlerImpl() {
        super(true);
    }

    public JdbcDCErrorHandlerImpl(boolean b) {
        super(b);
    }

    private static final int INVALID_USERNAME_PASSWORD_ORACLE_ERROR = 1017;
    private static final int ACCOUNT_LOCKED_ORACLE_ERROR = 28000;
    private static final int NO_SUITABLE_DRIVER = 0;
    private static final int NETWORK_CONNECTION_ERROR = 17002;
    private static final int NETWORK_ADAPTOR_ERROR = 20;

    public static boolean isFailedDBConnectErrorCode(SQLException s) {
    
        int errorCode = s.getErrorCode();
        return (errorCode == INVALID_USERNAME_PASSWORD_ORACLE_ERROR || errorCode == ACCOUNT_LOCKED_ORACLE_ERROR ||
                errorCode == NO_SUITABLE_DRIVER || errorCode == NETWORK_CONNECTION_ERROR ||
                errorCode == NETWORK_ADAPTOR_ERROR);
    }

    @Override
    public void reportException(DCBindingContainer formBnd, Exception e) {
        super.reportException(formBnd, e);
        if (e instanceof DMLException) {
            Object[] details = ((DMLException)e).getDetails();
            if (details != null && details.length > 0) {
                if (details[0] instanceof SQLException) {
                    SQLException s = (SQLException)details[0];
                    int errorCode = s.getErrorCode();
                    if (isFailedDBConnectErrorCode(s)) {
                        markResponseCompleteIfUsingJSF();
                        /* FacesContext fctx = FacesContext.getCurrentInstance();
                        UIViewRoot newPage =
                        fctx.getApplication().getViewHandler().createView(fctx,
                        "/error.html");
                        fctx.setViewRoot(newPage);
                        fctx.renderResponse();
                    
                        FacesContext context = FacesContext.getCurrentInstance();
                        HttpSession session  = (HttpSession)context.getExternalContext().getSession(false); */
                        runJavaScriptCode("alert(\"My name is Java\");");

                    
                    
                        System.out.println("DML EXCEPTION : __________________~~~~~~~~~~________");
                        //throw (DMLException)e;
                    
                    }
                }
            }
        }
    }

    /*
* If we are running in a Faces environment, invoke the FacesContext.responseComplete() method after
* the session invalidate. We use Java reflection so that our code can still work in a Non-Faces environment, too.
*/

    private void markResponseCompleteIfUsingJSF() {
        try {
            Class c = JBOClass.forName("javax.faces.context.FacesContext");
            Method m = c.getMethod("getCurrentInstance", null);
            Object obj = m.invoke(null, null);
            if (obj != null) {
                m = c.getMethod("responseComplete", null);
                m.invoke(obj, null);
            }
        } catch (InvocationTargetException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        } catch (ClassNotFoundException ex) {
            // Ignore, we're not running in a faces context.
        }
    }


    public String getDisplayMessage(BindingContext ctx, Exception ex) {
        System.out.println("CONNECTION LOST! _____ UPPER LIMIT");
        String message = "";
        if (ex instanceof DMLException) {
            message = "CONNECTION LOST!";
        }
        return message;
    }
// i used this method to show a javascript message 
    public static void runJavaScriptCode(String javascriptCode) {
             FacesContext facesCtx = FacesContext.getCurrentInstance();
             System.out.println("FACESCONTEXT : "+facesCtx.getViewRoot().toString());
             ExtendedRenderKitService service =
             Service.getRenderKitService(facesCtx, ExtendedRenderKitService.class);
             System.out.println("SERVICE : "+service);
             service.addScript(facesCtx, javascriptCode);
           }
}

If i use this custom class. And when error comes, then the sop statement i.e. "System.out.println("DML EXCEPTION : __________________~~~~~~~~~~________");" gets printed on, but if i try to call the java script , its not wrking.

I need sumthing so that i can show a message to the user in the jspx page that the following problem have occured.

I also tried using the error page concept in JDev, which is defined in web.xml file so that i can redirect the user to an error page if the desired exception orrurs. But its not working even if the exception occurs.
I have used the following code in web.xml

<error-page>
    <exception-type>oracle.jbo.DMLException</exception-type>
    <location>/error.html</location>
  </error-page>
  <error-page>
    <exception-type>java.sql.SQLRecoverableException</exception-type>
    <location>/error.html</location>
  </error-page>




But its not working as expected. I am using JDev 11.1.2.1.0image

Message was edited by: AmaanJunaid

This discussion has been closed.