I need to do some specific action after the successful transaction commit, which basically includes my analytical operations about the data;
I tried using the following code snippet
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));
}
}
But found that by when my the order is not persisted.
Can somebody tell me a better solution where I can trigger some action after transaction commit is successful.
I heard(tried using) about @TransactionEventListener, but didnt see anything triggering.
Updated the source
@Component
public class TXEventHandler {
@TransactionalEventListener
public void doAfterCommit(ApplicationEvent event){
//process here
}
}
About the application Its a Spring MVC based on 4.2.0 and uses Hibernate and MySql as the db.
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.
Thanks In Advance.