spring boot事务管理器建议回滚所有异常

时间:2017-12-15 19:36:01

标签: java spring spring-boot spring-data-jpa spring-transactions

在Spring Boot之前,我们可以全局说(无需在每个@Transactional元素上识别)所有已检查的异常都应该回滚事务:

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" rollback-for="Exception" timeout="20"/>
    </tx:attributes>
</tx:advice>

如何在Spring Boot世界中通过Java配置实现上述目标?

1 个答案:

答案 0 :(得分:2)

使用自定义注释

  

如果您发现重复使用相同的属性   @Transactional对许多不同的方法,然后是Spring的   元注释支持允许您定义自定义快捷方式   针对特定用例的注释。例如,定义   注释

  @Target({ElementType.METHOD, ElementType.TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  @Transactional(rollbackFor=Exception.class)
  public @interface OrderTx {
  }

https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-custom-attributes