SpringBoot 中 托管给Spring的事务中 手动回滚事务
托管给Spring的事务,一般是在方法结束时会自动提交事务,
在方法执行过程中如果抛出的 RuntimeException,Spring会自动回滚该事务。
但我们需要方法执行过程中 手动回滚事务,解决方法如下:
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@Transactional
public void test(CheckResult cr){
TestPO testPO = dao.findAll().get(0);
testPO.setName("#####");
dao.save(cr,testPO);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
浙公网安备 33010602011771号