事物和异常
一、dal 与 spring
1.org.springframework.transaction.annotation.Transactional
@Transactional(rollbackFor = Exception.class)
可以捕获异常:RpcException
2.com.yhcs.framework.dal.transaction.annotation
无法捕获异常:RpcException;只能捕获到它的上级:RuntimeException
@Transactional

- 实验一
不加rollbackFor属性,抛出RuntimeException,正常回滚
@Transactional
public void save(){
StudentDO studentDO = new StudentDO();
studentDO.setName("ltm");
studentDO.setAge(22);
studentMapper.insert(studentDO);
throw new RuntimeException("我是异常");
}
- 实验二
不加rollbackFor属性,抛出IOException,不回滚
@Transactional
public void save() throws IOException{
StudentDO studentDO = new StudentDO();
studentDO.setName("ltm");
studentDO.setAge(22);
studentMapper.insert(studentDO);
throw new IOException();
}
- 实验三
加上rollbackFor = Exception.class,抛出IOException,正常回滚
@Transactional(rollbackFor = Exception.class)
public void save() throws IOException{
StudentDO studentDO = new StudentDO();
studentDO.setName("ltm");
studentDO.setAge(22);
studentMapper.insert(studentDO);
throw new IOException();
}
- 实验四
不加rollbackFor属性,抛出OutOfMemoryError,正常回滚
@Transactional()
public void save(){
StudentDO studentDO = new StudentDO();
studentDO.setName("ltm");
studentDO.setAge(22);
studentMapper.insert(studentDO);
throw new OutOfMemoryError();
}
- 实验五
加上rollbackFor = Exception.class,抛出OutOfMemoryError,正常回滚,
说明rollbackFor = Exception.class不会覆盖Error的回滚
@Transactional(rollbackFor = Exception.class)
public void save(){
StudentDO studentDO = new StudentDO();
studentDO.setName("ltm");
studentDO.setAge(22);
studentMapper.insert(studentDO);
throw new OutOfMemoryError();
}
2975

被折叠的 条评论
为什么被折叠?



