我有一个EJB拦截器,它应该捕获并处理在事务中抛出的所有异常:
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);
}
}
它可以工作,但看起来像变通办法,我不喜欢这种解决方案。所以基本上我需要在事务之外运行拦截器。我该怎么办?