public class ExceptionsInterceptor {
@AroundInvoke
public Object intercept(final InvocationContext invocationContext) throws Exception {
try {
return invocationContext.proceed();
} catch (Exception e) {
throw new MyException(e);
}
}
}
但是如果在冬眠期间抛出冲水
PesistenceException
因为违反了约束,我无法捕捉到这个异常。我明白了
在我的拦截器完成工作后,那个冬眠者就冲了出来。
为了实现这一点,我用其他EJB装饰了这个EJB。
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED_NEW)
public class TargetServiceBean implements TargetServiceLocal {
@Override
public boolean method1(Integer digit) {
.. some code which my generate exception..
}
}
@Stateless
@Interceptors(ExceptionsInterceptor.class)
public class TargetServiceBean implements TargetServiceDecorator {
@Inject
private TargetServiceLocal service;
@Override
public boolean method1(Integer digit) {
service.method1(digit);
}
}