title: Pass object between before and after advice
body: <p>My question is the same as in <a href="https://stackoverflow.com/questions/23339457/pass-object-between-before-and-after-advice">Pass object between before and after advice?</a>, however the accepted answer doesn't work in my case and I would like to ask for some clarification on this topic.</p>
  
  <p>Actually, I'm using <code>around</code> advice, but I have to change it. Let's consider an example:</p>
  
  <pre><code>public Object registerLog( ProceedingJoinPoint jpoint)
  {
      SomeObject so = getSomeData( jpoint.getArgs());
      Object result = jpoint.proceed();
      getMoreData( result, so);
      log( so);
  }
  </code></pre>
  
  <p>My <code>proceed()</code> method is inserting object into database. In this scenario I'm logging the result of <code>INSERT</code> before its transaction commits, which is not very good. That's why I want to split <code>around</code> advice into <code>before</code> and <code>after</code>, so I can log in <code>after</code>, when the record is already in database. However, I still need to get some data of the object before it's stored in database.</p>
  
  <p>Of course the solution must be thread-safe, so I can't just add a <code>SomeObject</code> member to my aspect class. From the linked question I learned about <code>ThreadLocal</code>, and so far it's the best solution I found. However, I never used it before and I would like to ask if there are any other solutions. Maybe there's a way to force a transaction to commit while processing an aspect (I'm not sure right now if this is even a good idea)?</p>
  
  <p><strong>UPDATE with more details</strong></p>
  
  <p>I have two pointcuts:</p>
  
  <pre><code>&lt;aop:pointcut id="mngrPointcut" expression="execution(* com.mngr.Foo.*(..))"/&gt;
  &lt;aop:pointcut id="savePointcut" expression="execution(* com.mngr.Foo.save(..))"/&gt;
  </code></pre>
  
  <p>First pointcut has advice</p>
  
  <pre><code>&lt;aop:advisor advice-ref="txAdvice" pointcut-ref="mngrPointcut"/&gt;
  </code></pre>
  
  <p>where advice is</p>
  
  <pre><code>&lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="select*,search*,get*" propagation="REQUIRED" rollback-for="Exception" read-only="true"/&gt;
          &lt;tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  </code></pre>
  
  <p>For second pointcut I have aspect</p>
  
  <pre><code>&lt;aop:aspect ref="logAspect"&gt;
      &lt;aop:around method="registerLog" pointcut-ref="savePointcut"/&gt;
  &lt;/aop:aspect&gt;
  </code></pre>
  
  <p>When calling <code>com.mngr.Foo.save()</code> both pointcuts are valid. Probably because of declaration order, <code>mngrPointcut</code> is triggered first, starting a new transaction. Then <code>savePointcut</code> is triggered, starting an aspect. As I mentioned above, aspect is responsible for logging. Because of the order in which pointcuts are triggered, object is logged before it is stored in database, which is wrong. I thought that I have to use <code>before</code> and <code>after</code> advice instead of <code>around</code>, so I can call <code>log()</code> method in <code>after</code> part, when the transaction is already comitted.</p>