Spring boot 手动开启事务 手动提交 手动回滚
直接上代码
@Service
public class XXXService {
// 这两个必须要注入
@Resource
DataSourceTransactionManager dataSourceTransactionManager;
@Resource
TransactionDefinition transactionDefinition;
public void method1(){
// 开启事务
TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
try{
//提交事务
dataSourceTransactionManager.commit(transactionStatus);
} catch (Exception e) {
// 回滚事务
dataSourceTransactionManager.rollback(transactionStatus);
}
}
}