一件小事
用了下@Transactional
注解,发现异常出现后事务并没有回滚,service
层代码大概长这样
@Transactional
public void saveStudentInTryCatchBlock() {
try {
Student student = new Student();
student.setAge(12);
student.setName("testWT" + 1);
mapper.insertStudent(student);
student.setName("testWT" + 2);
mapper.insertStudent(student);
throw new Exception();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
回过神来,没有抛出方法外的异常当然捕捉不了,所以应该大约是这样写
@Transactional
public void saveStudentInTryCatchBlock() throws Exception{
try {
Student student = new Student();
student.setAge(12);
student.setName("testWT" + 1);
mapper.insertStudent(student);
student.setName("testWT" + 2);
mapper.insertStudent(student);
throw new Exception();
} catch (Exception e) {
System.out.println(e.getMessage());
//在这干些什么
throw e;
}
}
大概就是AOP编程这这那那的