title: Events after Transaction Commit Spring JPA
body: <p>I need to do some specific action after the successful transaction commit, which basically includes my analytical operations about the data;</p>
  
  <p>I tried using the following code snippet</p>
  
  <pre><code>public class EntityEventHandlers {
      private static Logger logger = LoggerFactory.getLogger(EntityEventHandlers.class);
  
      @Autowired
      private AsyncEventBus async;
  
      @PostUpdate
      public void postUpdate(Order order) {
          AutowireHelper.autowire(this, this.async);
          logger.debug("post update called " + order.getId());
          async.post(new ESCreateOrderEvent(order));
      }
  
      @PostPersist
      public void postPersist(Order order) {
          AutowireHelper.autowire(this, this.async);
          logger.debug("post insert called " + order.getId());
          async.post(new ESCreateOrderEvent(order));
      }
  }
  </code></pre>
  
  <p>But found that by when my the order is not persisted.</p>
  
  <p>Can somebody tell me a better solution where I can trigger some action after transaction commit is successful.</p>
  
  <p>I heard(tried using) about @TransactionEventListener, but didnt see anything triggering.</p>
  
  <blockquote>
    <p><strong>Updated the source</strong></p>
  </blockquote>
  
  <pre><code>@Component
  public class TXEventHandler {
      @TransactionalEventListener
      public void doAfterCommit(ApplicationEvent event){
          //process here
      }
  }
  </code></pre>
  
  <p>About the application
  Its a Spring MVC based on 4.2.0 and uses Hibernate and MySql as the db.</p>
  
  <p>Right now I have solved the problem by putting the event into a delay queue and the delay is sufficient to happen the db commit. But I know its not a good solution. So let me know if someone have faced this issue and able to fix it.</p>
  
  <p>Thanks In Advance.</p>