Transactional有那些失效场景或者长事务的的产生解决方案
失效场景
1
2
@Override public int insert(IntEndScaleForecastEntity entity) { entity.setId(UUID.randomUUID().toString()); // 模拟执行业务 System.out.println("1"); System.out.println("2"); publicTestInsert(entity); System.out.println("3"); if (1 == 1) { throw new RuntimeException("方法会不会事务失效"); } return 1; }
/ * 事务是不是失效了 ?为什么会失效 * @param entity * @return */ @Transactional(rollbackFor = Exception.class) public void publicTestInsert(IntEndScaleForecastEntity entity) { int result = iIntEndScaleForecastMapper.insertIgnoreNull(entity); System.out.println(result); }
数据库数据 变化情况。未回滚
解决方法:
1
- 可以将方法放入另一个类,如新增 manager层,通过spring注入,这样符合了在对象之间调用的条件。
2
在最上层方法上 添加注解
@Transactional(rollbackFor = Exception.class) public int insert(IntEndScaleForecastEntity entity) { entity.setId(UUID.randomUUID().toString()); // 模拟执行业务 System.out.println("1"); System.out.println("2"); publicTestInsert(entity); System.out.println("3"); if (1 == 1) { throw new RuntimeException("方法会不会事务失效"); } return 1; }
持续更新。。。。。。
创作打卡挑战赛
赢取流量/现金/CSDN周边激励大奖ZDfans