此处不解释事务隔离级别以及传播行为了哦。
有多种方式吧
1、如果是使用了@Transactional注释 那么可实现
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
public void suspend() { } public void resume() { } public void flush() { } public void beforeCommit(boolean readOnly) { } public void beforeCompletion() { } public void afterCompletion(int status) { }
@Override public void afterCommit() { // 发送任务消息 }
}
2、手动提交事务
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Autowired private DataSourceTransactionManager transactionManager;
public void method(){
//手动开启事务 DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition(); transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus transStatus = transactionManager.getTransaction(transDefinition);
try { //手动提交事务 transactionManager.commit(transStatus); } catch (Exception e) { log.error("delStrategy error {}", ExceptionUtils.getStackTrace(e)); //事务回滚 transactionManager.rollback(transStatus); throw new BizCommonException(StrategyErrorCode.DELETE_ERROR); }
}