spring事物传播行为怎么设置

不及物动词 其他 62

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    Spring事务传播行为可以通过事务注解或者编程式事务管理来进行设置。

    1. 事务注解方式:
      使用@Transactional注解在方法上设置事务传播行为。常用的传播行为有以下几种:
    • PROPAGATION_REQUIRED:默认的传播行为,如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。
    • PROPAGATION_REQUIRES_NEW:每次都创建一个新的事务,如果当前存在事务,则挂起该事务。
    • PROPAGATION_NESTED:如果当前存在事务,则在嵌套事务中执行;如果当前没有事务,则创建一个新的事务。
    • PROPAGATION_SUPPORTS:如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务方式执行。
    • PROPAGATION_NOT_SUPPORTED:以非事务方式进行执行,如果当前存在事务,则挂起该事务。
    • PROPAGATION_NEVER:以非事务方式进行执行,如果当前存在事务,则抛出异常。

    示例代码:

    @Transactional(propagation = Propagation.REQUIRED)
    public void doSomething() {
        // 方法体
    }
    
    1. 编程式事务方式:
      通过编程的方式设置事务传播行为,可以使用TransactionTemplate或者PlatformTransactionManager来管理事务。
      示例代码:
    // 使用TransactionTemplate
    @Autowired
    TransactionTemplate transactionTemplate;
    
    public void doSomething() {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // 方法体
            }
        });
    }
    
    // 使用PlatformTransactionManager
    @Autowired
    PlatformTransactionManager transactionManager;
    
    public void doSomething() {
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    
        TransactionStatus status = transactionManager.getTransaction(definition);
    
        try {
            // 方法体
    
            transactionManager.commit(status);
        } catch (Exception e) {
            transactionManager.rollback(status);
            throw e;
        }
    }
    

    以上就是关于Spring事务传播行为设置的方法,根据具体业务场景选择合适的传播行为即可。

    1年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    在Spring中,事务传播行为用于定义在多个事务方法被调用时,如何传播事务的状态。事务传播行为可以通过配置或编程方式进行设置。下面是设置Spring事务传播行为的几种方式:

    1. 使用注解:
      在使用注解的方式配置事务时,可以使用@Transactional注解来设置事务的传播行为。@Transactional注解有一个属性是propagation,用于设置事务的传播行为。常用的传播行为包括:

      • REQUIRED:如果当前存在事务,则加入到当前事务中,否则创建一个新的事务
      • REQUIRES_NEW:创建一个新的事务,并挂起当前的事务
      • SUPPORTS:如果当前存在事务,则加入到当前事务中,否则以非事务的方式执行
      • NOT_SUPPORTED:以非事务的方式执行,如果当前存在事务,则挂起当前事务
      • NEVER:以非事务的方式执行,如果当前存在事务,则抛出异常

      示例代码如下:

      @Transactional(propagation = Propagation.REQUIRED)
      public void methodA() {
          // 方法A的事务传播行为为REQUIRED
          // 执行业务逻辑
      }
      
      @Transactional(propagation = Propagation.REQUIRES_NEW)
      public void methodB() {
          // 方法B的事务传播行为为REQUIRES_NEW
          // 执行业务逻辑
      }
      
    2. 使用XML配置:
      在使用XML配置的方式配置事务时,可以使用tx:advice元素来设置事务的传播行为。常用的传播行为配置与注解方式相同。

      示例代码如下:

      <tx:advice id="txAdvice">
          <tx:attributes>
              <tx:method name="methodA" propagation="REQUIRED" />
              <tx:method name="methodB" propagation="REQUIRES_NEW" />
          </tx:attributes>
      </tx:advice>
      

      这里使用tx:advice元素配置一个名为txAdvice的事务通知,然后通过tx:attributes子元素来配置各个方法的传播行为。

    3. 使用编程方式:
      除了使用注解和XML配置外,还可以使用编程方式来设置事务的传播行为。通过编程方式设置事务传播行为时,可以使用TransactionDefinition接口的常量来表示不同的传播行为,如:

      • TransactionDefinition.PROPAGATION_REQUIRED:REQUIRED传播行为
      • TransactionDefinition.PROPAGATION_REQUIRES_NEW:REQUIRES_NEW传播行为
      • TransactionDefinition.PROPAGATION_SUPPORTS:SUPPORTS传播行为
      • TransactionDefinition.PROPAGATION_NOT_SUPPORTED:NOT_SUPPORTED传播行为
      • TransactionDefinition.PROPAGATION_NEVER:NEVER传播行为

      示例代码如下:

      @Autowired
      private PlatformTransactionManager transactionManager;
      
      public void methodA() {
          TransactionDefinition txDef = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
          TransactionStatus txStatus = transactionManager.getTransaction(txDef);
      
          try {
              // 执行业务逻辑
              transactionManager.commit(txStatus);
          } catch (Exception e) {
              transactionManager.rollback(txStatus);
          }
      }
      
      public void methodB() {
          TransactionDefinition txDef = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
          TransactionStatus txStatus = transactionManager.getTransaction(txDef);
      
          try {
              // 执行业务逻辑
              transactionManager.commit(txStatus);
          } catch (Exception e) {
              transactionManager.rollback(txStatus);
          }
      }
      

      这里使用PlatformTransactionManager接口来获取事务,并使用TransactionDefinition接口的实现类DefaultTransactionDefinition来设置传播行为。

    4. 设置默认的传播行为:
      还可以通过配置默认的事务传播行为,这样在没有显式设置传播行为的方法调用时,就会使用默认的传播行为。

      使用注解配置默认的传播行为:

      @Configuration
      @EnableTransactionManagement
      public class AppConfig implements TransactionManagementConfigurer {
      
          @Override
          public PlatformTransactionManager annotationDrivenTransactionManager() {
              return transactionManager();
          }
      
          @Bean
          public PlatformTransactionManager transactionManager() {
              // 配置事务管理器
          }
      
          @Bean
          public TransactionAttributeSource transactionAttributeSource() {
              AnnotationTransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource();
              // 配置默认的传播行为
              attributeSource.setDefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
              return attributeSource;
          }
      
          @Bean
          public TransactionInterceptor transactionInterceptor() {
              TransactionInterceptor interceptor = new TransactionInterceptor();
              interceptor.setTransactionAttributeSource(transactionAttributeSource());
              return interceptor;
          }
      }
      

      使用XML配置默认的传播行为:

      <tx:annotation-driven transaction-manager="transactionManager" default-rollback-for="Exception">
          <tx:attributes>
              <tx:method name="*" propagation="REQUIRED" />
          </tx:attributes>
      </tx:annotation-driven>
      

      配置中的default-rollback-for属性用于设置出现异常时的默认回滚策略,默认为Exception。

    5. 动态设置传播行为:
      在某些情况下,我们可能需要根据实际需求来动态设置事务的传播行为。可以通过编程方式来实现:

      @Autowired
      private PlatformTransactionManager transactionManager;
      
      public void dynamicTransactionalMethod(boolean newTransaction) {
          TransactionDefinition txDef;
          if (newTransaction) {
              txDef = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
          } else {
              txDef = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
          }
      
          TransactionStatus txStatus = transactionManager.getTransaction(txDef);
      
          try {
              // 执行业务逻辑
              transactionManager.commit(txStatus);
          } catch (Exception e) {
              transactionManager.rollback(txStatus);
          }
      }
      

      这里根据传入的boolean参数来决定是否使用REQUIRES_NEW传播行为。

    总结:
    在Spring中,可以通过注解、XML配置或编程方式来设置事务的传播行为。根据实际的需求,可以选择合适的方式来配置事务的传播行为。同时,还可以设置默认的传播行为,以及动态设置传播行为。要根据具体的业务逻辑和需求来选择合适的传播行为,以保证事务的一致性和可靠性。

    1年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    Spring框架提供了丰富的事务管理功能,其中之一是事务的传播行为。事务的传播行为决定了在多个事务方法调用时,如何处理事务的边界。Spring框架定义了7种事务的传播行为,可以根据具体的业务需求进行设置。

    1. REQUIRED:如果当前存在事务,则加入该事务,如果当前没有事务,则创建一个新的事务。这是默认的传播行为。

    2. SUPPORTS:如果当前存在事务,则加入该事务,如果当前没有事务,则以非事务方式运行。

    3. MANDATORY:如果当前存在事务,则加入该事务,如果当前没有事务,则抛出异常。

    4. REQUIRES_NEW:创建一个新的事务,并挂起当前事务(如果有)。

    5. NOT_SUPPORTED:以非事务方式运行,并挂起当前事务(如果有)。

    6. NEVER:以非事务方式运行,如果当前存在事务,则抛出异常。

    7. NESTED:如果当前存在事务,则在当前事务的嵌套事务中执行,如果当前没有事务,则创建一个新的事务。

    要设置事务的传播行为,在Spring配置文件中使用@Transactional注解或者XML配置。

    1. 使用@Transactional注解

    在方法上添加@Transactional注解,并设置propagation属性为对应的传播行为。例如:

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateData() {
        // 业务逻辑
    }
    
    1. 使用XML配置

    在Spring配置文件中添加事务管理器和事务模板的配置,并在需要的方法上添加tx:method配置。例如:

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="updateData" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.example.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>
    

    上述配置中,将updateData方法的传播行为设置为REQUIRED。

    在选择事务的传播行为时,需要根据具体的业务需求进行考虑。不同的传播行为可能会产生不同的事务边界和事务管理行为,需要根据业务逻辑的要求来确定。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部