kikione
V2EX  ›  Java

spring 事务问题

  •  1
     
  •   kikione · Nov 5, 2021 · 4021 views
    This topic created in 1825 days ago, the information mentioned may be changed or developed.

    两个 Service A 的方法 A 没有加 @Transactional 和 Service B ;

    Service A 的方法 A 没有加 @Transactional ;

    Service B 的方法 B 添加 @Transactional ;

    方法 A 调用方法 B ,方法 B 出错,抛出全局异常,

    为什么会去回滚到 controller 里,再执行一次。

    相当于 B 抛出全局异常后, 这个接口执行了两遍

    Supplement 1  ·  Nov 5, 2021
    两个 Service A 和 Service B ;

    第一句
    17 replies  •  2021-11-15 14:09:59 +08:00
    Hurriance
    Hurriance
    Nov 5, 2021
    1
    回滚到 controller ? debug 的过程是怎样的
    kikione
    kikione
    OP
    Nov 5, 2021
    2
    @Hurriance debug 就是 抛出异常后,又从 controller 执行一遍,我把 @Transactional 放在方法 A ,就不会重复执行了,直接返回异常
    kikione
    kikione
    OP
    Nov 5, 2021
    3
    @Hurriance 不太清楚什么原理
    Hurriance
    Hurriance
    Nov 5, 2021
    4
    可以的话,贴个代码吧
    thetbw
    thetbw
    Nov 5, 2021
    5
    应该是代码写的问题的,看看是不是接口哪里掉了两次,事务仅仅是数据库层面的回滚吧
    nonoyang
    nonoyang
    Nov 5, 2021 via iPhone❤️5
    6
    刚下班,我他妈连第一句都看不懂……
    tty0
    tty0
    Nov 5, 2021
    7
    没下班, 第一句我也看不懂......
    kikione
    kikione
    OP
    Nov 5, 2021
    8
    @nonoyang 第一句写错了 我的
    clf
    clf
    Nov 5, 2021
    9
    你怎么判断 Controller 接口执行了两次的?

    另外建议贴代码。
    siweipancc
    siweipancc
    Nov 5, 2021 via iPhone
    10
    控制层打印一下当前线程 id
    NutChocHoney
    NutChocHoney
    Nov 5, 2021
    11
    断点放掉的一瞬间又有人走这个方法了吧
    yqsas
    yqsas
    Nov 5, 2021 via iPad
    12
    猜测 serviceA 那边开启了调用异常,自动重试功能
    cp19890714
    cp19890714
    Nov 6, 2021
    13
    你提出了两个问题。
    1. 事务回滚到了 controller ,也就是说 A 中的数据也回滚了。
    2. controller 执行了两次

    回答:
    1. 请说明你的证据
    2. 最大可能就是异常触发重试
    LeeSeoung
    LeeSeoung
    Nov 6, 2021
    14
    大概率是用的框架做了重试。。
    leatomic
    leatomic
    Nov 7, 2021
    15
    只能说 talk is cheap 了
    tedzhou1221
    tedzhou1221
    Nov 8, 2021
    16
    你可以 debug 一下,看看整个调用栈的过程
    notwaste
    notwaste
    Nov 15, 2021
    17
    盲猜用了 spring retry
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1245 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 22:17 · PVG 06:17 · LAX 14:17 · JFK 17:17
    ♥ Do have faith in what you're doing.