springboot手动开启事务
手动开启事务,手动提交/回滚
@Resource
DataSourceTransactionManager dataSourceTransactionManager;
public void updateMonitorRecord() {
taskExecutor.execute(() -> {
//开启事务
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);//创建一个新事务
TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(definition);
try {
//do something 业务代码
//提交
dataSourceTransactionManager.commit(transactionStatus);
} catch (Exception e) {
//手动回滚
dataSourceTransactionManager.rollback(transactionStatus);
}
});
}
作者:cchilei
-------------------------------------------
个性签名:竹杖芒鞋轻胜马 一蓑烟雨任平生
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!