Skip to content
Advertisement

How to get exception inside ActionInvocation.invoke()

First of all the final purpose is that i’m trying to inject a DAO connection into an SMD context (Ajax) so i’ll ensure that transactions are being Commited (or Rollback), my problem is that i’m not being able to know if the invoke() method throws an exception,

I have the following Interceptor:

public class SomeInterceptor implements Interceptor {
    private static final long serialVersionUID = 1L;
    @Override
    public String intercept(ActionInvocation invocation) {
        String result = "";
        GenericDAO dao = new GenericDAO();
        try {
            dao.begin();
            invocation.getInvocationContext().put("contextDao", dao);
            result = invocation.invoke();
            //attempt to solve
            Object exception = invocation.getInvocationContext().getValueStack().findValue("exception");
            if (exception != null && exception instanceof Exception){
                dao.rollback();
            } else {
                dao.commit();
            }
        } catch (Exception ex) {
            dao.rollback();
            System.out.println("ROLLBACK!");
            ex.printStackTrace();
        } finally {
            dao.close();
        }
        return result;
    }
}

The line “attempt to solve” is based on this question. Inside the invoke i’m just throwing a NullPointerException, the result right now is that the exception is being catch before the catch at the Interceptor, however is not a catch that i had set,

@SMDMethod
public HashMap<String,String> someMethod() {
    IGenericDAO dao = (IGenericDAO) ActionContext.getContext().get("contextDao");
    //dao's deletes, updates that i want to rollback
    HashMap<String,String> x = null;
    x.put("x","x"); //<---- NPE!
    return null;
}

I want ActionInvocation.invoke() to throw the exception so i’ll know i need to rollback the DB session. Any approach who succeed this purpose is welcome,

Edit 1: I’ve found this question that does almost the same as me but i dont understand how is using rollback (at my point of view is always doing rollback)

Greetings

Advertisement

Answer

I didn’t found any way to achieve my goal as i wanted, instead i’ve solved the scenario by doing this:

public class SomeInterceptor implements Interceptor {
    private static final long serialVersionUID = 1L;
    @Override
    public String intercept(ActionInvocation invocation) {
        String result = "";
        GenericDAO dao = new GenericDAO();
        try {
            dao.begin();
            invocation.getInvocationContext().put("contextDao", dao);
            result = invocation.invoke();
            dao.rollback();
        } catch (Exception ex) {
            dao.rollback();
            System.out.println("ROLLBACK!");
            ex.printStackTrace();
        } finally {
            dao.close();
        }
        return result;
    }
}

Yes… i removed “commit” instruction at interceptor, instead now im forced to do “commit” at the end of any call that uses the mentioned DAO,

@SMDMethod
public HashMap<String,String> someMethod() {
    IGenericDAO dao = (IGenericDAO) ActionContext.getContext().get("contextDao");
    //dao's deletes, updates that i want to rollback
    HashMap<String,String> x = null;
    x.put("x","x"); //<---- NPE!
    dao.commit();   //<---- COMMIT!
    return null;
}

I dont like this solution but it was all i was able to do. I came out with this like 1 day after posted the question, i waited for an answer until now,

Hope it helps someone,

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement