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