springboot.已经自动配置了@EnableTransactionManagement,不需要在启动类加 @EnableTransactionManagement,
//第一种方式:注解事务回滚
@Override
@Transactional(rollbackFor = Exception.class)//第一种方式
public Boolean insert(String param) throws Exception {
try {
int i = testDao.insert(test);
if(i!=1){
throw new exception("新增失败");
}
return true;
} catch (Exception e) {
log.error("新增异常",e);
return false;
}
}
//第二种方式:手动事务回滚
@Override
public Boolean insert2(String param) {
try {
int i = testDao.insert(test);
if(i!=1){
throw new exception("新增失败");
}
return true;
} catch (Exception e) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//第二种方式
log.error("新增异常",e);
return false;
}
}