Java EE Interceptor导致事务回滚

时间:2017-03-02 14:20:38

标签: java-ee transactions ejb interceptor

我正在玩Java EE。这是我的监控拦截器:

public class MonitoringInterceptor {

    @AroundInvoke
    public Object monitorMethodCall(InvocationContext ctx) throws java.lang.Exception {
        Exception exception = null;
        long start = System.currentTimeMillis();  // FIXME: use nanotime
        try {
            return ctx.proceed();
        } catch (Exception e){
            exception = e;
            throw e; // <- PROBLEM
        } finally {
            long duration = System.currentTimeMillis() - start;
            log(ctx, duration, exception );
        }
    }

    [...]
}

然后我在我的所有豆子中使用这个拦截器:

@Stateless
@Interceptors({MonitoringInterceptor.class})
public class BeanA {
   [...]
}

@Stateless
@Interceptors({MonitoringInterceptor.class})
public class BeanB {

   @EJB
   private BeanA beanA;

   public void doit(){
       try {
          beanA.dosomeThing();
       } catch(Exception e) {

          // handle...
       }
   }
}

现在问题是,如果我调用方法BeanB#doit()并且BeanA#dosomeThing()抛出异常,我的拦截器将捕获异常以记录错误,传递它然后我的事务得到滚动返回 [!]。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

如果您不希望tx支持不使用EJB或使用NotSupported EJB方法进行批注,这是正常行为。