spring boot 事务管理器建议回滚所有异常 [英] spring boot transaction manager advice to roll back all exceptions

查看:41
本文介绍了spring boot 事务管理器建议回滚所有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Prior to Spring Boot, one was able to globally say (without having to identify on each @Transactional element) that all checked exceptions should roll back a transaction:

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

在 Spring Boot 的世界中是如何通过 Java config 实现的?

How is the above achieved via Java config in the Spring Boot world?

推荐答案

使用自定义注解

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

If you find you are repeatedly using the same attributes with @Transactional on many different methods, then Spring's meta-annotation support allows you to define custom shortcut annotations for your specific use cases. For example, defining the following annotations

  @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

这篇关于spring boot 事务管理器建议回滚所有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆