spring中@Transactional注解使用时注意点总结

来源:互联网 发布:程序员的自我修养 知乎 编辑:程序博客网 时间:2024/06/10 03:46

@Transactional注解默认是对unchecked异常进行起作用

如果也想对checked异常进行使用,则需要如下设置

@Transactional(rollbackFor=Exception.class)
    public void addCustomer(ECustomer customer)throws Exception {
        
        System.out.println("开始保存客户");
        try {
            
            EStudent student = new EStudent("小强");
                ETeacher t1 = new ETeacher("张老师");
                t1.setStudents(student);
                
            baseDao.save(t1);
            
            
            //System.out.println(s);
        } catch (Exception e) {
            
            System.out.println("11111111111111111111");
            //throw new RuntimeException("dfdsfdsf");
        }
        int s = 5/0;
        //throw new Exception("非运行时异常....");
        //System.out.println("保存客户成功...");
        
    }