/** * @Author:qxy */ //@Service public class StudentServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper; @Autowired private ClassService classService; @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void insertClassByException(StudentDo studentDo) throws CustomException { studentMapper.insertStudent(studentDo); throw new CustomException(); } }
@Service public class ClassServiceImpl implements ClassService { @Autowired private ClassMapper classMapper; public void insertClass(ClassDo classDo) throws CustomException { insertClassByException(classDo); } @Override @Transactional(propagation = Propagation.REQUIRED) public void insertClassByException(ClassDo classDo) throws CustomException { classMapper.insertClass(classDo); throw new RuntimeException(); } } //测试用例: @Test public void insertInnerExceptionTest() throws CustomException { classDo.setClassId(2); classDo.setClassName("java_2"); classDo.setClassNo("java_2"); classService.insertClass(classDo); }
java.lang.RuntimeException at com.qxy.common.service.impl.ClassServiceImpl.insertClassByException(ClassServiceImpl.java:34) at com.qxy.common.service.impl.ClassServiceImpl.insertClass(ClassServiceImpl.java:27) at com.qxy.common.service.impl.ClassServiceImpl$$FastClassBySpringCGLIB$$a1c03d8.invoke(<generated>)
public void insertClass(ClassDo classDo) throws CustomException { // insertClassByException(classDo); ((ClassServiceImpl)AopContext.currentProxy()).insertClassByException(classDo); } //测试用例: @Test public void insertInnerExceptionTest() throws CustomException { classDo.setClassId(3); classDo.setClassName("java_3"); classDo.setClassNo("java_3"); classService.insertClass(classDo); }
java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available. at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:69) at com.qxy.common.service.impl.ClassServiceImpl.insertClass(ClassServiceImpl.java:28)
@Service public class ClassServiceImpl implements ClassService { @Autowired private ClassMapper classMapper; // @Override // @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class) public void insertClass(ClassDo classDo) throws Exception { // 即使此处使用代理对象调用内部事务方法,数据依然未发生回滚,事务机制亦然失效 ((ClassServiceImpl)AopContext.currentProxy()).insertClassByException(classDo); } @Override @Transactional(propagation = Propagation.REQUIRED) public void insertClassByException(ClassDo classDo) throws Exception { classMapper.insertClass(classDo); //抛出非RuntimeException类型 throw new Exception(); } //测试用例: @Test public void insertInnerExceptionTest() throws Exception { classDo.setClassId(3); classDo.setClassName("java_3"); classDo.setClassNo("java_3"); classService.insertClass(classDo); } }
java.lang.Exception at com.qxy.common.service.impl.ClassServiceImpl.insertClassByException(ClassServiceImpl.java:35) at com.qxy.common.service.impl.ClassServiceImpl$$FastClassBySpringCGLIB$$a1c03d8.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
@Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void insertClassByException(ClassDo classDo) throws Exception { classMapper.insertClass(classDo); throw new Exception(); }
@Service public class ClassServiceImpl implements ClassService { @Autowired private ClassMapper classMapper; // @Override public void insertClass(ClassDo classDo) { ((ClassServiceImpl)AopContext.currentProxy()).insertClassByException(classDo); } @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void insertClassByException(ClassDo classDo) { classMapper.insertClass(classDo); try { int i = 1 / 0; } catch (Exception e) { e.printStackTrace(); } } } // 测试用例: @Test public void insertInnerExceptionTest() { classDo.setClassId(4); classDo.setClassName("java_4"); classDo.setClassNo("java_4"); classService.insertClass(classDo); }
@Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void insertClassByException(ClassDo classDo) { classMapper.insertClass(classDo); try { int i = 1 / 0; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } }
@Transactional(propagation = Propagation.NOT_SUPPORTED,rollbackFor = Exception.class) public void insertClassByException(ClassDo classDo) { classMapper.insertClass(classDo); try { int i = 1 / 0; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } }
END 十期推荐 【241期】面试官:你了解JVM中的ZGC垃圾收集器吗? 【242期】面试官:Spring AOP有哪些通知类型,它们的执行顺序是怎样的? 【243期】面试官:什么是前缀索引、为什么要用前缀使用、用在什么场景下? 【244期】万字 图解 Redis,面试不用愁了! 【245期】面试官:MySQL发生死锁有哪些原因,怎么避免? 【246期】面试官:说说你对 RabbitMQ 的理解以及使用它的场景 【247期】记一次Java面试中遇到的三个问题及感悟! 【248期】面试官:你能说几个Java8中Stream对列表去重的方法吗? 【249期】关于Java中的异常,面试可以问的都在这里了! 【250期】关于Mybatis知识点,面试可以问的都在这里了! ? ~