zoukankan      html  css  js  c++  java
  • Spring Transaction Propagation

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11530611.html

    REQUIRED behavior

    Spring REQUIRED behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context. If there is no existing transaction the Spring container will create a new one. If multiple methods configured as REQUIRED behavior are called in a nested way they will be assigned distinct logical transactions but they will all share the same physical transaction. In short this means that if an inner method causes a transaction to rollback, the outer method will fail to commit and will also rollback the transaction. Let's see an example:

    Outer bean

    @Autowired
    private TestDAO testDAO;
    
    @Autowired
    private InnerBean innerBean;
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void testRequired(User user) {
      testDAO.insertUser(user);
      try{
        innerBean.testRequired();
      } catch(RuntimeException e){
        // handle exception
      }
    }

    Inner bean

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void testRequired() {
      throw new RuntimeException("Rollback this transaction!");
    }

    Note that the inner method throws a RuntimeException and is annotated with REQUIRED behavior. This means that it will use the same transaction as the outer bean, so the outer transaction will fail to commit and will also rollback.

    REQUIRES_NEW behavior

    REQUIRES_NEW behavior means that a new physical transaction will always be created by the container. In other words the inner transaction may commit or rollback independently of the outer transaction, i.e. the outer transaction will not be affected by the inner transaction result: they will run in distinct physical transactions.

    Outer bean

    @Autowired
    private TestDAO testDAO;
    
    @Autowired
    private InnerBean innerBean;
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void testRequiresNew(User user) {
      testDAO.insertUser(user);
      try{
        innerBean.testRequiresNew();
      } catch(RuntimeException e){
        // handle exception
      }
    }

    Inner bean

    @Override
    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void testRequiresNew() {
      throw new RuntimeException("Rollback this transaction!");
    }

    The inner method is annotated with REQUIRES_NEW and throws a RuntimeException so it will set its transaction to rollback but will not affect the outer transaction. The outer transaction is paused when the inner transaction starts and then resumes after the inner transaction is concluded. They run independently of each other so the outer transaction may commit successfully.

    Reference

    http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html

    http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial

  • 相关阅读:
    MiracleSnow网页设计HTML5+CSS3+JS全套视频教程
    Java开发者应该列入年度计划的5件事
    大爱HTML5 9款超炫HTML5最新动画源码
    10个PHP代码片段
    斯坦福大学即将上线10门公开课
    如何成为一个偷懒又高效的Android开发人员
    github版本库使用详细教程
    PHP5中PDO的简单使用
    Github 已经托管超过 1000 万个项目库
    DIOCP转发型中间网关
  • 原文地址:https://www.cnblogs.com/agilestyle/p/11530611.html
Copyright © 2011-2022 走看看