2 Replies Latest reply on Feb 17, 2008 2:21 PM by Pete Muir

    Exception handling with interceptors

    Nicklas Karlsson Master


      I have most reuseable database queries isolated to a DAOish class and would like to add some logging and exception handling like

      Object result = null;
      try {
       result = context.proceed();
       // log called methods, parameters and result
      } catch (PersistenceException e) {
       // get error context from invocation context and lookup
       // from message bundles etc...
      return result;

      The problem however is that the connection seems to have gone sour anyway, which leads to ugly "Transaction is not active" errors in my long running conversation.

      Is there any way to recover from this? Do you have any experience with using interceptors for centralized error logging/management and recovery?

      Thanks in advance,

        • 1. Re: Exception handling with interceptors
          Jan te Beest Newbie

          I've been using Interceptors for this kind of work. However one can only define EJB3 interceptors at the class level.

          What I do is a workaround, which I find rather nasty, but since I haven't thought of another way yet and this works, I'll point it out.

          I define an annotation which has Interceptors defined on it, with targets TYPE and METHOD. I put it on the class where I want a method to be intercepted and put it on the method(s) to be intercepted inside that class as well.

          In the aroundInvoke, I do a check on that same annotation being present on the calling method. Something like the following:

           public Object aroundInvoke(InvocationContext ctx) {
           Object result = null;
           if(ctx.getMethod().getAnnotation(SomeAnnotation.class) != null) {
           try {
           result = ....
           // The rest of your code
           catch( ... ) {
           return result;

          Might not be the most elegant method, but it works for me.

          I hope that helps you!


          • 2. Re: Exception handling with interceptors
            Pete Muir Master

            When do you mean the transaction has gone sour? after the exception? This is expected - you need a new transaction at this point.