失效场景
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;
}
持续更新。。。。。。