在service层模拟事务回滚,出于编码习惯添加try catch语句捕获异常,结果事务并没有回滚。
try {
int template = 5/0;
} catch (ArithmeticException e) {
System.out.println("算术异常.........");
e.printStackTrace();
}
事务能够回滚是因为程序抛出运行时异常,而很明显ArithmeticException 就是。
解决方法:
去掉try catch语句,再跑一遍程序,事务成功捕获异常,回滚。