zoukankan      html  css  js  c++  java
  • 事物的七种传播行为

    事务传播行为类型

    事务传播行为类型

    说明

    PROPAGATION_REQUIRED

    如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

    PROPAGATION_SUPPORTS

    支持当前事务,如果当前没有事务,就以非事务方式执行。

    PROPAGATION_MANDATORY

    使用当前的事务,如果当前没有事务,就抛出异常。

    PROPAGATION_REQUIRES_NEW

    新建事务,如果当前存在事务,把当前事务挂起。

    PROPAGATION_NOT_SUPPORTED

    以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

    PROPAGATION_NEVER

    以非事务方式执行,如果当前存在事务,则抛出异常。

    PROPAGATION_NESTED

    如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类 似的操作。

    罗列了7种spring的事务传播行为,我们具体来看看它的实现。在这里,我们使用spring annotation注解实现事务。

    实现事务的类BusinessServiceImpl

    package com.aop;  
      
    import org.springframework.aop.support.AopUtils;  
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.stereotype.Service;  
    import org.springframework.transaction.annotation.Isolation;  
    import org.springframework.transaction.annotation.Propagation;  
    import org.springframework.transaction.annotation.Transactional;  
      
    import com.entity.Student;  
      
    @Service("businessSerivce")  
    public class BusinessServiceImpl implements IBaseService {  
      
        @Autowired  
        IStudentDao studentDao;  
      
        @Autowired  
        IBaseServiceB baseServiceb;  
      
        @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)  
        public String doA() throws Exception {  
            Student st = new Student();  
            st.setId(1);  
            st.setSex("girl");  
            st.setUsername("zx");  
            studentDao.insertStudent(st);  
      
            System.out.println(baseServiceb);  
            System.out.println("是否是代理调用,AopUtils.isAopProxy(baseServiceb) : " + AopUtils.isAopProxy(baseServiceb));  
            System.out  
                    .println("是否是cglib类代理调用,AopUtils.isCglibProxy(baseServiceb) : " + AopUtils.isCglibProxy(baseServiceb));  
            System.out.println("是否是jdk动态接口代理调用,AopUtils.isJdkDynamicProxy(baseServiceb) : "  
                    + AopUtils.isJdkDynamicProxy(baseServiceb));  
              
            //使用代理调用方法doB()  
            baseServiceb.doB();  
            int i = 1 / 0;// 抛出异常,doB()的事务事务回滚  
            return "success";  
        }  
      
        // @Transactional(propagation = Propagation.REQUIRES_NEW, isolation =  
        // Isolation.DEFAULT, rollbackFor = Exception.class)  
        // public String doB() throws Exception {  
        // Student st = new Student();  
        // st.setId(2);  
        // st.setSex("girl");  
        // st.setUsername("zx2");  
        // studentDao.insertStudent(st);  
        //  
        // return "success";  
        // }  
      
    }  


    实现事务的类BusinessServiceImpl

    package com.aop;  
      
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.stereotype.Service;  
    import org.springframework.transaction.annotation.Isolation;  
    import org.springframework.transaction.annotation.Propagation;  
    import org.springframework.transaction.annotation.Transactional;  
      
    import com.entity.Student;  
      
    @Service("businessSerivceB")  
    public class BusinessServiceImplB implements IBaseServiceB {  
        
        @Autowired  
        IStudentDao studentDao;  
          
        @Transactional(propagation = Propagation.REQUIRES_NEW,isolation=Isolation.DEFAULT,rollbackFor=Exception.class)    
        public String doB() throws Exception {  
            Student st = new Student();  
            st.setId(2);  
            st.setSex("girl");  
            st.setUsername("zx2");  
            studentDao.insertStudent(st);  
            return "success";  
        }  
           
    }

      


    测试类:

    package com.aop;  
      
    import org.springframework.beans.factory.BeanFactory;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    import com.thread.service.IBaseFacadeService;  
      
      
    //@RunWith(SpringJUnit4ClassRunner.class)  
    //@ContextConfiguration(locations = { "classpath:/spring-ibatis.xml", "classpath:/spring-jdbctemplate.xml" })  
    public class TestStudentDao {  
      
        public static void main(String[] args) {  
              try {  
              BeanFactory factory = new ClassPathXmlApplicationContext("spring-jdbctemplate.xml");  
              IBaseService service = (IBaseService) factory.getBean("businessSerivce");  
              
                  service.doA();  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
    }  

    测试结果:

    om.aop.BusinessServiceImplB@14b75fd9
    是否是代理调用,AopUtils.isAopProxy(baseServiceb) : true
    是否是cglib类代理调用,AopUtils.isCglibProxy(baseServiceb) : true
    是否是jdk动态接口代理调用,AopUtils.isJdkDynamicProxy(baseServiceb) : false

  • 相关阅读:
    MYSQL
    数据中台的思考与总结
    基于SpringCloud分布式架构
    java常用工具库使用
    String性能提升10倍的几个方法
    Validator 注解使用
    【数据结构】trie树
    几种排序算法小结
    Core data的batch update
    UIScrollView和UIPageControl结合实现图片浏览
  • 原文地址:https://www.cnblogs.com/baixingqiang/p/6023944.html
Copyright © 2011-2022 走看看