Announcement Announcement Module
No announcement yet.
Advice executed twice Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Advice executed twice

    For one of the methods in our service layer, we would like to implement:

    - Declarative transaction management
    - Email notification upon successful execution of the method

    We have done this using Spring, and our configuration contains the following:

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:method name="*"/>
      <aop:pointcut id="someOperation" expression="execution(* someInterface.someMethod(..))"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="someOperation"/>
      <aop:aspect id="successfulExecutionNotification" ref="emailNotifier">
        <aop:pointcut id="successfulExecution" expression="execution(* someInterface.someMethod(argType1, argType2)) and args(arg1, arg2)"/>                                               
        <aop:after-returning pointcut-ref="successfulExecution" method="sendEmail"/>
    Everything seems to work fine, except that emailNotifier.sendEmail() gets invoked twice.

    Does anyone know why this might be happening, and how to work around it?

    We have tried using a reference to the "someOperation" pointcut inside the "successfulExecutionNotification" aspect (instead of defining a new pointcut inline), but whenever we try to specify the arguments in the "someOperation" pointcut, Spring gives us initialization errors.

    It seems that a pointcut with argument specifications can only be used inside an <aop:aspect>.

    We also tried defining the pointcut inline inside the <aop:advisor> instead of referring to the "someOperation" pointcut, but this didn't fix the problem of sendEmail() being invoked twice.

    Thanks in advance.

  • #2
    I thought a lot about the same stuff today and think that the problem maybe is related to the use of dynamic proxies. It seems that all calls are running on the proxyobject first and afterwards on the object itself, so that each call runs at least twice. I've also tried a lot but was not able to solve the problem and using an Advice for logging is quite difficult if each call appears twice in the logs (or if a mail will be send two times). So no idea what to do. But if you (or anybody else) finds a solution, it would be great to hear about the way to go.



    • #3
      When using the xml style of Spring AOP you cannot refer to parameterized pointcuts. Inline the "successfulExecution" pointcut and things will work as expected.


      • #4
        Hmm, actually it looks like it *is* possible to use 'sort-of parameterized' pointcuts in XML style - contrary to what the manual says.

        I'm not sure why you're seeing multiple calls to your advice method.


        • #5
          Hi all,
          I've also checked that out with aspectj Annotations but got the same effect also without using parameterized pointcuts. Using Spring AOP without aspectj also leads to calling the advice up to 6 or 7 times and with using aspectj I was able to reduce those calls down to two. After stepping through the app with a Debugger I'm almost sure that the problem occurs because of some proxyobjects being generated because the main method mentioned in the pointcut was called only one time as expected so that the the number of calls seems really "magic" to me.



          • #6
            Can you provide a minimal testcase? It would help if you replaced the <tx:advice> with some custom advice (class implementing MethodInterceptor for example).


            • #7
              Hi again,
              thanks a lot for making me think about the problem again because a really simple error causes a lot of trouble :o)
              In my log4j.xml I'v inserted a logging of the Interceptor instead of logging the class or package defined as a pointcut. This leads to the curious result that every occurence has been logged twice so that I suppose my problem isn't the same like the problem of ilem. So sorry about boardering this thread and thanks for pushing me back to this topic and learning a lot about AOP.

              bye + a nice day I hope


              • #8
                No problem. Making mistakes is human