V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JasonLaw
V2EX  ›  Java

@Transactional VS @Transactional(rollbackFor = Exception.class)

  •  1
     
  •   JasonLaw · 2021-04-17 14:18:25 +08:00 · 2792 次点击
    这是一个创建于 1098 天前的主题,其中的信息可能已经有所发展或是发生改变。

    到底什么场景会使用到Transactional(rollbackFor = Exception.class)

    额外阅读

    17 条回复    2021-04-19 10:07:28 +08:00
    Paosin
        1
    Paosin  
       2021-04-17 15:19:31 +08:00
    学到了😂
    siweipancc
        2
    siweipancc  
       2021-04-17 16:43:34 +08:00 via iPhone
    ……不明所以
    JasonLaw
        3
    JasonLaw  
    OP
       2021-04-17 16:51:19 +08:00
    @siweipancc #2 ?
    xuanbg
        4
    xuanbg  
       2021-04-17 16:59:59 +08:00
    这篇文章里面的示例,调用同一个类的方法,事务不起作用的吧。
    siweipancc
        5
    siweipancc  
       2021-04-17 17:00:12 +08:00 via iPhone
    @JasonLaw :D 又重看了一次,我还是看不懂
    crclz
        6
    crclz  
       2021-04-17 17:02:42 +08:00
    根据 https://www.cnblogs.com/clwydjgs/p/9317849.html:

    在 @Transactional 注解中如果不配置 rollbackFor 属性,那么事物只会在遇到 RuntimeException 的时候才会回滚,加上 rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚。

    可以理解是,假设你始终使用 RuntimeException (遵循 kotlin 和 C#的风格),那么就只需要用 @Transactional 。我的建议是:Exception 有害,请遵循 kotlin 和 C#的风格。
    JasonLaw
        7
    JasonLaw  
    OP
       2021-04-17 17:06:36 +08:00
    @xuanbg #4 虽然 transferMoney 调用了同一个 class 中的方法,但是是外部调用 transferMoney 的,所以`@Transactional`是起作用的。
    JasonLaw
        8
    JasonLaw  
    OP
       2021-04-17 17:07:07 +08:00
    @siweipancc #5 你说的看是看什么😅?
    JasonLaw
        9
    JasonLaw  
    OP
       2021-04-17 17:11:26 +08:00
    @crclz #6 所以我一直在纠结,在被`@Transactional`标注的方法上,什么时候会声明`throws Exception`?
    securityCoding
        10
    securityCoding  
       2021-04-17 17:16:38 +08:00
    推荐看一下 @Transactional 的原理.
    本质上 jdbc 是通过捕捉异常来判断是否需要回滚,所以才有指定异常回滚这种用法
    gdtdpt
        11
    gdtdpt  
       2021-04-17 17:26:32 +08:00
    @JasonLaw 偷懒的时候吧,比如逻辑部分会 throw 多种 Exception 子类,不想在 throws 里写全,直接写个 Exception
    jin7
        12
    jin7  
       2021-04-17 17:37:44 +08:00
    @transactional(rollbackfor = throwable.class) 有没有必要?
    taogen
        13
    taogen  
       2021-04-17 21:17:46 +08:00   ❤️ 1
    @jin7 #12

    "In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration."

    - https://docs.spring.io/spring-framework/docs/3.2.6.RELEASE/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back

    1. default rollback for unchecked exceptions
    2. Errors will also - by default - result in a rollback

    So recommend specifying rollback for Excpetion class.
    JasonLaw
        14
    JasonLaw  
    OP
       2021-04-17 21:57:02 +08:00
    @taogen #13 "So recommend specifying rollback for Excpetion class."是在哪里出现的?没有找到
    bxb100
        15
    bxb100  
       2021-04-17 23:34:25 +08:00 via Android
    一般都是全捕捉然后回滚
    taogen
        16
    taogen  
       2021-04-18 22:25:19 +08:00 via Android
    @JasonLaw 这句话是我自己总结的 😁
    ychost
        17
    ychost  
       2021-04-19 10:07:28 +08:00
    我一直觉得 Java 的 checked Exception 有点鸡肋,看上去很能帮助用户意识到方法可能会抛什么 exception,但是真实的工程项目里面用着贼操蛋
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1001 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 22:46 · PVG 06:46 · LAX 15:46 · JFK 18:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.