在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ SpringBoot事務(wù)回滾

SpringBoot事務(wù)回滾

1、Controller代碼

   @PostMapping("/add")
   public RestResult add(ModelParamInputDTO modelParamDTO) {
       this.isValid("modelName", modelParamDTO.getModelName());
       this.isValid("drugId", modelParamDTO.getDrugId());
       this.isValid("drugCode", modelParamDTO.getDrugCode());
       aiModelService.addAiModel(modelParamDTO);
       return ResultGenerator.genSuccessResult().setMessage("保存成功");
   }
   
   @PostMapping("/isValid")
   public RestResult isValid(@RequestParam String key, String value) {
       Boolean isValid = aiModelService.isValid(key, value);
       if (isValid) {
           return ResultGenerator.genSuccessResult();
       } else {
           String[] ret = {""};
           VALID_MESSAGE.forEach((k, v) -> {
               if (k.equals(key)) {
                   ret[0] = v;
               }
           });
           throw new ServiceException(ret[0]);
       }
   }

2、service代碼

@Override
@Transactional(rollbackFor=Exception.class)
public void addAiModel(ModelParamInputDTO modelParamDTO) {

    String modelId = CoreUtils.getUUID();
    AiModel aiModel = new AiModel();
    BeanUtils.copyProperties(modelParamDTO, aiModel);
    aiModel.setId(modelId);
    aiModel.setOrgId(ORG_ID);
    aiModel.setGmtCreate(new Date());
    aiModel.setModelName(modelParamDTO.getDrugName());
    aiModelMapper.insert(aiModel);
    String s = null;
    int d = s.length();               
}

3、Application代碼

@SpringBootApplication
@MapperScan("cn.itpower.pms.modules.**.dao")
@EnableConfigurationProperties
@EnableTransactionManagement
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

代碼如上,我自己設(shè)置了一個空指針異常,但是事務(wù)沒有回滾,我試過:

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
結(jié)果事務(wù)還是沒有回滾,請問究竟是怎么回事呢?

回答
編輯回答
艷骨

mysql的話,數(shù)據(jù)庫引擎需要設(shè)置innodb

2017年2月6日 02:24
編輯回答
焚音

你需要貼出的是調(diào)用addAiModel方法的那部分代碼

2017年9月27日 17:24
編輯回答
來守候

可以看看這個例子項目:https://github.com/chanjarste...
希望對你有用

2017年8月28日 13:35
編輯回答
別傷我

在Application上是否添加了注解

@EnableTransactionManagement
2017年9月24日 14:01
編輯回答
胭脂淚

能確定下你的表是innoDB嗎

ALTER TABLE `database名`.`表名`
ENGINE = InnoDB;

比如mysql建一個Database test,下面的表既可以是myisam 也可以是innodb

clipboard.png

2018年5月13日 11:02
編輯回答
神經(jīng)質(zhì)
   @PostMapping("/add")
   public RestResult add(ModelParamInputDTO modelParamDTO) {
       this.isValid("modelName", modelParamDTO.getModelName());
       this.isValid("drugId", modelParamDTO.getDrugId());
       this.isValid("drugCode", modelParamDTO.getDrugCode());
       aiModelService.addAiModel(modelParamDTO);
       return ResultGenerator.genSuccessResult().setMessage("保存成功");
   }

如上代碼可能存在問題,是否和缺少注解@RequestBody有關(guān)?
另外,Controller不要使用dto接收參數(shù),使用VO。

2018年8月25日 10:40