@Async("threadPoolExecutor")
@EventListener(SaveOrderSuccessEvent.class)
public void onApplicationEvent(SaveOrderSuccessEvent event) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
// 在事务提交之后执行的代码
PlaceOrderVo placeOrderVo = event.getPlaceOrderVo();
// ... 其他逻辑
cleanOrderService.handleCleanOrder(placeOrderVo);
}
});
}
或者直接使用
@Async(“threadPoolExecutor”)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onApplicationEvent(SaveOrderSuccessEvent event) {
// 在事务提交之后执行的代码
PlaceOrderVo placeOrderVo = event.getPlaceOrderVo();
// … 其他逻辑
cleanOrderService.handleCleanOrder(placeOrderVo);
}