分布式事务解决方案之TCC

Posted dalianpai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分布式事务解决方案之TCC相关的知识,希望对你有一定的参考价值。

什么是TCC事务
TCC是Try、Confirm、Cancel三个词语的缩写,TCC要求每个分支事务实现三个操作:预处理Try、确认
Confirm、撤销Cancel。Try操作做业务检查及资源预留,Confirm做业务确认操作,Cancel实现一个与Try相反的
操作即回滚操作。TM首先发起所有的分支事务的try操作,任何一个分支事务的try操作执行失败,TM将会发起所
有分支事务的Cancel操作,若try操作全部成功,TM将会发起所有分支事务的Confirm操作,其中Confirm/Cancel
操作若执行失败,TM会进行重试。

技术图片

 分支事务失败的情况:

技术图片

TCC分为三个阶段:
1. Try 阶段是做业务检查(一致性)及资源预留(隔离),此阶段仅是一个初步操作,它和后续的Confirm 一起才能
真正构成一个完整的业务逻辑。

2. Confirm 阶段是做确认提交,Try阶段所有分支事务执行成功后开始执行 Confirm。通常情况下,采用TCC则
认为 Confirm阶段是不会出错的。即:只要Try成功,Confirm一定成功。若Confirm阶段真的出错了,需引
入重试机制或人工处理。
3. Cancel 阶段是在业务执行错误需要回滚的状态下执行分支事务的业务取消,预留资源释放。通常情况下,采
用TCC则认为Cancel阶段也是一定成功的。若Cancel阶段真的出错了,需引入重试机制或人工处理。
4. TM事务管理器
TM事务管理器可以实现为独立的服务,也可以让全局事务发起方充当TM的角色,TM独立出来是为了成为公
用组件,是为了考虑系统结构和软件复用。
TM在发起全局事务时生成全局事务记录,全局事务ID贯穿整个分布式事务调用链条,用来记录事务上下文,
追踪和记录状态,由于Confirm 和cancel失败需进行重试,因此需要实现为幂等,幂等性是指同一个操作无论请求
多少次,其结果都相同。

Hmily框架介绍

Hmily是一个高性能分布式事务TCC开源框架。基于Java语言来开发(JDK1.8),支持Dubbo,Spring Cloud等
RPC框架进行分布式事务。它目前支持以下特性:
支持嵌套事务(Nested transaction support).
采用disruptor框架进行事务日志的异步读写,与RPC框架的性能毫无差别。
支持SpringBoot-starter 项目启动,使用简单。
RPC框架支持 : dubbo,motan,springcloud。
本地事务存储支持 : redis,mongodb,zookeeper,file,mysql
事务日志序列化支持 :java,hessian,kryo,protostuff。
采用Aspect AOP 切面思想与Spring无缝集成,天然支持集群。
RPC事务恢复,超时异常恢复等。
Hmily利用AOP对参与分布式事务的本地方法与远程方法进行拦截处理,通过多方拦截,事务参与者能透明的
调用到另一方的Try、Confirm、Cancel方法;传递事务上下文;并记录事务日志,酌情进行补偿,重试等。

Hmily不需要事务协调服务,但需要提供一个数据库(mysql/mongodb/zookeeper/redis/file)来进行日志存
储。
Hmily实现的TCC服务与普通的服务一样,只需要暴露一个接口,也就是它的Try业务。Confirm/Cancel业务
逻辑,只是因为全局事务提交/回滚的需要才提供的,因此Confirm/Cancel业务只需要被Hmily TCC事务框架
发现即可,不需要被调用它的其他业务服务所感知。
官网介绍:https://dromara.org/website/zh-cn/docs/hmily/index.html
TCC需要注意三种异常处理分别是空回滚、幂等、悬挂:

空回滚:
在没有调用 TCC 资源 Try 方法的情况下,调用了二阶段的 Cancel 方法,Cancel 方法需要识别出这是一个空回
滚,然后直接返回成功。
出现原因是当一个分支事务所在服务宕机或网络异常,分支事务调用记录为失败,这个时候其实是没有执行Try阶
段,当故障恢复后,分布式事务进行回滚则会调用二阶段的Cancel方法,从而形成空回滚。
解决思路是关键就是要识别出这个空回滚。思路很简单就是需要知道一阶段是否执行,如果执行了,那就是正常回
滚;如果没执行,那就是空回滚。前面已经说过TM在发起全局事务时生成全局事务记录,全局事务ID贯穿整个分
布式事务调用链条。再额外增加一张分支事务记录表,其中有全局事务 ID 和分支事务 ID,第一阶段 Try 方法里会
插入一条记录,表示一阶段执行了。Cancel 接口里读取该记录,如果该记录存在,则正常回滚;如果该记录不存
在,则是空回滚。
幂等:
通过前面介绍已经了解到,为了保证TCC二阶段提交重试机制不会引发数据不一致,要求 TCC 的二阶段 Try、
Confirm 和 Cancel 接口保证幂等,这样不会重复使用或者释放资源。如果幂等控制没有做好,很有可能导致数据
不一致等严重问题。
解决思路在上述“分支事务记录”中增加执行状态,每次执行前都查询该状态。

悬挂:
悬挂就是对于一个分布式事务,其二阶段 Cancel 接口比 Try 接口先执行。
出现原因是在 RPC 调用分支事务try时,先注册分支事务,再执行RPC调用,如果此时 RPC 调用的网络发生拥堵,
通常 RPC 调用是有超时时间的,RPC 超时以后,TM就会通知RM回滚该分布式事务,可能回滚完成后,RPC 请求
才到达参与者真正执行,而一个 Try 方法预留的业务资源,只有该分布式事务才能使用,该分布式事务第一阶段预
留的业务资源就再也没有人能够处理了,对于这种情况,我们就称为悬挂,即业务资源预留后没法继续处理。
解决思路是如果二阶段执行完成,那一阶段就不能再继续执行。在执行一阶段事务时判断在该全局事务下,“分支
事务记录”表中是否已经有二阶段事务记录,如果有则不执行Try。

举例,场景为 A 转账 30 元给 B,A和B账户在不同的服务。
方案1:
账户A

try:
检查余额是否够30元    
扣减30元    
   
confirm:
空    
cancel:
增加30元 

账户B

try:
增加30元    
confirm:
空    
cancel:
减少30元 

方案1说明:
1)账户A,这里的余额就是所谓的业务资源,按照前面提到的原则,在第一阶段需要检查并预留业务资源,因此,
我们在扣钱 TCC 资源的 Try 接口里先检查 A 账户余额是否足够,如果足够则扣除 30 元。 Confirm 接口表示正式
提交,由于业务资源已经在 Try 接口里扣除掉了,那么在第二阶段的 Confirm 接口里可以什么都不用做。Cancel
接口的执行表示整个事务回滚,账户A回滚则需要把 Try 接口里扣除掉的 30 元还给账户。
2)账号B,在第一阶段 Try 接口里实现给账户B加钱,Cancel 接口的执行表示整个事务回滚,账户B回滚则需要把
Try 接口里加的 30 元再减去。
方案1的问题分析:
1)如果账户A的try没有执行在cancel则就多加了30元。
2)由于try,cancel、confirm都是由单独的线程去调用,且会出现重复调用,所以都需要实现幂等。
3)账号B在try中增加30元,当try执行完成后可能会其它线程给消费了。
4)如果账户B的try没有执行在cancel则就多减了30元。
问题解决:
1)账户A的cancel方法需要判断try方法是否执行,正常执行try后方可执行cancel。
2)try,cancel、confirm方法实现幂等。
3)账号B在try方法中不允许更新账户金额,在confirm中更新账户金额。
4)账户B的cancel方法需要判断try方法是否执行,正常执行try后方可执行cancel。

优化方案:
账户A

try:
try幂等校验    
try悬挂处理    
检查余额是否够30元    
扣减30元    
confirm:
空    
cancel:
cancel幂等校验    
cancel空回滚处理    
增加可用余额30元 

账户B

try:
空    
confirm:
confirm幂等校验    
正式增加30元    
cancel:
空  

Hmily实现TCC事务
本实例通过Hmily实现TCC分布式事务,模拟两个账户的转账交易过程。
两个账户分别在不同的银行(张三在bank1、李四在bank2),bank1、bank2是两个微服务。交易过程是,张三给
李四转账指定金额。
上述交易步骤,要么一起成功,要么一起失败,必须是一个整体性的事务。

技术图片

创建数据库
导入数据库脚本:资料sqlank1.sql、资料sqlank2.sql、已经导过不用重复导入。
创建hmily数据库,用于存储hmily框架记录的数据。

技术图片

创建bank1库,并导入以下表结构和数据(包含张三账户)

DROP TABLE IF EXISTS `account_info`;
CREATE TABLE `account_info`  (
  `id` bigint(20NOT NULL AUTO_INCREMENT,
  `account_name` varchar(100CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 户
主姓名,
  `account_no` varchar(100CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 银行
卡号,
  `account_password` varchar(100CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT
帐户密码,
  `account_balance` double NULL DEFAULT NULL COMMENT 帐户余额,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT =
Dynamic;
INSERT INTO `account_info` VALUES (2张三的账户1‘‘10000);

创建bank2库,并导入以下表结构和数据(包含李四账户)

REATE DATABASE `bank2` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `account_info`  (
  `id` bigint(20NOT NULL AUTO_INCREMENT,
  `account_name` varchar(100CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 户
主姓名,
  `account_no` varchar(100CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 银行
卡号,
  `account_password` varchar(100CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT
帐户密码,
  `account_balance` double NULL DEFAULT NULL COMMENT 帐户余额,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT =
Dynamic;
INSERT INTO `account_info` VALUES (3李四的账户2NULL0);

每个数据库都创建try、confirm、cancel三张日志表:

CREATE TABLE `local_try_log` (
  `tx_no` varchar(64NOT NULL COMMENT 事务id,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`tx_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `local_confirm_log` (
  `tx_no` varchar(64NOT NULL COMMENT 事务id,
  `create_time` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `local_cancel_log` (
  `tx_no` varchar(64NOT NULL COMMENT 事务id,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`tx_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

bank1的配置

server: 
  port: 9903

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9900/eureka/

spring:
  ##################### DB #####################
  datasource:
    ds0:
      url: jdbc:mysql://localhost:3306/bank1?useUnicode=true
      username: root
      password: 123456
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT user()
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      connection-properties: druid.stat.mergeSql:true;druid.stat.slowSqlMillis:5000

org:
  dromara:
    hmily :
      serializer : kryo
      recoverDelayTime : 30
      retryMax : 30
      scheduledDelay : 30
      scheduledThreadMax :  10
      repositorySupport : db
      started: true
      hmilyDbConfig :
        driverClassName  : com.mysql.jdbc.Driver
        url :  jdbc:mysql://localhost:3306/hmily?useUnicode=true
        username : root
        password : 123456

logging:
  level:
    root: info
    org.springframework.web: info
    org.apache.ibatis: info
    org.dromara.hmily.bonuspoint: debug
    org.dromara.hmily.lottery: debug
    org.dromara.hmily: debug
    io.netty: info
    com.topcheer.hmily: debug

 

配置类

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class DatabaseConfiguration {


    private final ApplicationContext applicationContext;


    @Autowired
    private Environment env;

    public DatabaseConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.ds0")
    public DruidDataSource ds0() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }



    @Bean
    public HmilyTransactionBootstrap hmilyTransactionBootstrap(HmilyInitService hmilyInitService){
        HmilyTransactionBootstrap hmilyTransactionBootstrap = new HmilyTransactionBootstrap(hmilyInitService);
        hmilyTransactionBootstrap.setSerializer(env.getProperty("org.dromara.hmily.serializer"));
        hmilyTransactionBootstrap.setRecoverDelayTime(Integer.parseInt(env.getProperty("org.dromara.hmily.recoverDelayTime")));
        hmilyTransactionBootstrap.setRetryMax(Integer.parseInt(env.getProperty("org.dromara.hmily.retryMax")));
        hmilyTransactionBootstrap.setScheduledDelay(Integer.parseInt(env.getProperty("org.dromara.hmily.scheduledDelay")));
        hmilyTransactionBootstrap.setScheduledThreadMax(Integer.parseInt(env.getProperty("org.dromara.hmily.scheduledThreadMax")));
        hmilyTransactionBootstrap.setRepositorySupport(env.getProperty("org.dromara.hmily.repositorySupport"));
        hmilyTransactionBootstrap.setStarted(Boolean.parseBoolean(env.getProperty("org.dromara.hmily.started")));
        HmilyDbConfig hmilyDbConfig = new HmilyDbConfig();
        hmilyDbConfig.setDriverClassName(env.getProperty("org.dromara.hmily.hmilyDbConfig.driverClassName"));
        hmilyDbConfig.setUrl(env.getProperty("org.dromara.hmily.hmilyDbConfig.url"));
        hmilyDbConfig.setUsername(env.getProperty("org.dromara.hmily.hmilyDbConfig.username"));
        hmilyDbConfig.setPassword(env.getProperty("org.dromara.hmily.hmilyDbConfig.password"));
        hmilyTransactionBootstrap.setHmilyDbConfig(hmilyDbConfig);
        return hmilyTransactionBootstrap;
    }


    /*@Bean
    @ConfigurationProperties(prefix = "org.dromara.hmily")
    public HmilyConfig hmilyConfig(){
        return new HmilyConfig();
    }



    @Bean
    public HmilyTransactionBootstrap hmilyTransactionBootstrap(HmilyInitService hmilyInitService, HmilyConfig hmilyConfig){
        HmilyTransactionBootstrap hmilyTransactionBootstrap = new HmilyTransactionBootstrap(hmilyInitService);
        return hmilyTransactionBootstrap;
    }*/



}

 

Dao层

@Mapper
@Component
public interface AccountInfoDao {
    @Update("update account_info set account_balance=account_balance - #{amount} where account_balance>=#{amount} and account_no=#{accountNo} ")
    int subtractAccountBalance(@Param("accountNo") String accountNo, @Param("amount") Double amount);

    @Update("update account_info set account_balance=account_balance + #{amount} where account_no=#{accountNo} ")
    int addAccountBalance(@Param("accountNo") String accountNo, @Param("amount") Double amount);


    /**
     * 增加某分支事务try执行记录
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Insert("insert into local_try_log values(#{txNo},now());")
    int addTry(String localTradeNo);

    @Insert("insert into local_confirm_log values(#{txNo},now());")
    int addConfirm(String localTradeNo);

    @Insert("insert into local_cancel_log values(#{txNo},now());")
    int addCancel(String localTradeNo);

    /**
     * 查询分支事务try是否已执行
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Select("select count(1) from local_try_log where tx_no = #{txNo} ")
    int isExistTry(String localTradeNo);
    /**
     * 查询分支事务confirm是否已执行
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Select("select count(1) from local_confirm_log where tx_no = #{txNo} ")
    int isExistConfirm(String localTradeNo);

    /**
     * 查询分支事务cancel是否已执行
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Select("select count(1) from local_cancel_log where tx_no = #{txNo} ")
    int isExistCancel(String localTradeNo);

}

 

Service层

@Service
@Slf4j
public class AccountInfoServiceImpl implements AccountInfoService {

    @Autowired
    AccountInfoDao accountInfoDao;

    @Autowired
    Bank2Client bank2Client;

    // 账户扣款,就是tcc的try方法

    /**
     *     try幂等校验
     *     try悬挂处理
     *     检查余额是够扣减金额
     *     扣减金额
     * @param accountNo
     * @param amount
     */
    @Override
    @Transactional
    //只要标记@Hmily就是try方法,在注解中指定confirm、cancel两个方法的名字
    @Hmily(confirmMethod="commit",cancelMethod="rollback")
    public void updateAccountBalance(String accountNo, Double amount) {
        //获取全局事务id
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank1 try begin 开始执行...xid:{}",transId);
        //幂等判断 判断local_try_log表中是否有try日志记录,如果有则不再执行
        if(accountInfoDao.isExistTry(transId)>0){
            log.info("bank1 try 已经执行,无需重复执行,xid:{}",transId);
            return ;
        }

        //try悬挂处理,如果cancel、confirm有一个已经执行了,try不再执行
        if(accountInfoDao.isExistConfirm(transId)>0 || accountInfoDao.isExistCancel(transId)>0){
            log.info("bank1 try悬挂处理  cancel或confirm已经执行,不允许执行try,xid:{}",transId);
            return ;
        }

        //扣减金额
        if(accountInfoDao.subtractAccountBalance(accountNo, amount)<=0){
            //扣减失败
            throw new RuntimeException("bank1 try 扣减金额失败,xid:{}"+transId);
        }
        //插入try执行记录,用于幂等判断
        accountInfoDao.addTry(transId);

        //远程调用李四,转账
        if(!bank2Client.transfer(amount)){
            throw new RuntimeException("bank1 远程调用李四微服务失败,xid:{}"+transId);
        }
        if(amount == 2){
            throw new RuntimeException("人为制造异常,xid:{}"+transId);
        }
        log.info("bank1 try end 结束执行...xid:{}",transId);
    }

    //confirm方法
    @Transactional
    public void commit(String accountNo, Double amount){
        //获取全局事务id
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank1 confirm begin 开始执行...xid:{},accountNo:{},amount:{}",transId,accountNo,amount);
    }



    /** cancel方法
     *     cancel幂等校验
     *     cancel空回滚处理
     *     增加可用余额
     * @param accountNo
     * @param amount
     */
    @Transactional
    public void rollback(String accountNo, Double amount){
        //获取全局事务id
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank1 cancel begin 开始执行...xid:{}",transId);
        //    cancel幂等校验
        if(accountInfoDao.isExistCancel(transId)>0){
            log.info("bank1 cancel 已经执行,无需重复执行,xid:{}",transId);
            return ;
        }
        //cancel空回滚处理,如果try没有执行,cancel不允许执行
        if(accountInfoDao.isExistTry(transId)<=0){
            log.info("bank1 空回滚处理,try没有执行,不允许cancel执行,xid:{}",transId);
            return ;
        }
        //    增加可用余额
        accountInfoDao.addAccountBalance(accountNo,amount);
        //插入一条cancel的执行记录
        accountInfoDao.addCancel(transId);
        log.info("bank1 cancel end 结束执行...xid:{}",transId);

    }

}

feignClient

@FeignClient(value="tcc-demo-bank2",fallback=Bank2ClientFallback.class)
public interface Bank2Client {
    //远程调用李四的微服务
    @GetMapping("/bank2/transfer")
    @Hmily
    public  Boolean transfer(@RequestParam("amount") Double amount);
}

 

Web层

@RestController
public class Bank1Controller {
    @Autowired
    AccountInfoService accountInfoService;

    @RequestMapping("/transfer")
    public Boolean transfer(@RequestParam("amount") Double amount) {
        this.accountInfoService.updateAccountBalance("1", amount);
        return true;
    }

}

 

bank2

Dao

@Component
@Mapper
public interface AccountInfoDao {

    @Update("update account_info set account_balance=account_balance + #{amount} where  account_no=#{accountNo} ")
    int addAccountBalance(@Param("accountNo") String accountNo, @Param("amount") Double amount);


    /**
     * 增加某分支事务try执行记录
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Insert("insert into local_try_log values(#{txNo},now());")
    int addTry(String localTradeNo);

    @Insert("insert into local_confirm_log values(#{txNo},now());")
    int addConfirm(String localTradeNo);

    @Insert("insert into local_cancel_log values(#{txNo},now());")
    int addCancel(String localTradeNo);

    /**
     * 查询分支事务try是否已执行
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Select("select count(1) from local_try_log where tx_no = #{txNo} ")
    int isExistTry(String localTradeNo);
    /**
     * 查询分支事务confirm是否已执行
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Select("select count(1) from local_confirm_log where tx_no = #{txNo} ")
    int isExistConfirm(String localTradeNo);

    /**
     * 查询分支事务cancel是否已执行
     * @param localTradeNo 本地事务编号
     * @return
     */
    @Select("select count(1) from local_cancel_log where tx_no = #{txNo} ")
    int isExistCancel(String localTradeNo);

}

 

service

@Service
@Slf4j
public class AccountInfoServiceImpl implements AccountInfoService {

    @Autowired
    AccountInfoDao accountInfoDao;

    @Override
    @Hmily(confirmMethod="confirmMethod", cancelMethod="cancelMethod")
    public void updateAccountBalance(String accountNo, Double amount) {
        //获取全局事务id
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank2 try begin 开始执行...xid:{}",transId);
    }

    /**
     * confirm方法
     *     confirm幂等校验
     *     正式增加金额
     * @param accountNo
     * @param amount
     */
    @Transactional
    public void confirmMethod(String accountNo, Double amount){
        //获取全局事务id
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank2 confirm begin 开始执行...xid:{}",transId);
        if(accountInfoDao.isExistConfirm(transId)>0){
            log.info("bank2 confirm 已经执行,无需重复执行...xid:{}",transId);
            return ;
        }
        //增加金额
        accountInfoDao.addAccountBalance(accountNo,amount);
        //增加一条confirm日志,用于幂等
        accountInfoDao.addConfirm(transId);
        log.info("bank2 confirm end 结束执行...xid:{}",transId);
    }



    /**
     * @param accountNo
     * @param amount
     */
    public void cancelMethod(String accountNo, Double amount){
        //获取全局事务id
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank2 cancel begin 开始执行...xid:{}",transId);

    }

}

 

web层

@RestController
public class Bank2Controller {
    @Autowired
    AccountInfoService accountInfoService;

    @RequestMapping("/transfer")
    public Boolean transfer(@RequestParam("amount") Double amount) {
        this.accountInfoService.updateAccountBalance("2", amount);
        return true;
    }

}

 

测试

技术图片

2020-03-10 12:38:09.513 DEBUG 23404 --- [nio-9903-exec-6] o.d.h.c.s.e.HmilyTransactionExecutor     : ......hmily transaction starter....
2020-03-10 12:38:09.517  INFO 23404 --- [nio-9903-exec-6] c.t.h.s.impl.AccountInfoServiceImpl      : bank1 try begin 开始执行...xid:1237236275664916480
2020-03-10 12:38:09.521 DEBUG 23404 --- [nio-9903-exec-6] c.t.hmily.dao.AccountInfoDao.isExistTry  : ==>  Preparing: select count(1) from local_try_log where tx_no = ? 
2020-03-10 12:38:09.523 DEBUG 23404 --- [nio-9903-exec-6] c.t.hmily.dao.AccountInfoDao.isExistTry  : ==> Parameters: 1237236275664916480(String)
2020-03-10 12:38:09.526 DEBUG 23404 --- [nio-9903-exec-6] c.t.hmily.dao.AccountInfoDao.isExistTry  : <==      Total: 1
2020-03-10 12:38:09.527 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.dao.AccountInfoDao.isExistConfirm  : ==>  Preparing: select count(1) from local_confirm_log where tx_no = ? 
2020-03-10 12:38:09.528 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.dao.AccountInfoDao.isExistConfirm  : ==> Parameters: 1237236275664916480(String)
2020-03-10 12:38:09.529 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.dao.AccountInfoDao.isExistConfirm  : <==      Total: 1
2020-03-10 12:38:09.529 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.dao.AccountInfoDao.isExistCancel   : ==>  Preparing: select count(1) from local_cancel_log where tx_no = ? 
2020-03-10 12:38:09.529 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.dao.AccountInfoDao.isExistCancel   : ==> Parameters: 1237236275664916480(String)
2020-03-10 12:38:09.530 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.dao.AccountInfoDao.isExistCancel   : <==      Total: 1
2020-03-10 12:38:09.531 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.d.A.subtractAccountBalance         : ==>  Preparing: update account_info set account_balance=account_balance - ? where account_balance>=? and account_no=? 
2020-03-10 12:38:09.531 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.d.A.subtractAccountBalance         : ==> Parameters: 100.0(Double), 100.0(Double), 1(String)
2020-03-10 12:38:09.533 DEBUG 23404 --- [nio-9903-exec-6] c.t.h.d.A.subtractAccountBalance         : <==    Updates: 1
2020-03-10 12:38:09.534 DEBUG 23404 --- [nio-9903-exec-6] c.t.hmily.dao.AccountInfoDao.addTry      : ==>  Preparing: insert into local_try_log values(?,now()); 
2020-03-10 12:38:09.534 DEBUG 23404 --- [nio-9903-exec-6] c.t.hmily.dao.AccountInfoDao.addTry      : ==> Parameters: 1237236275664916480(String)
2020-03-10 12:38:09.536 DEBUG 23404 --- [nio-9903-exec-6] c.t.hmily.dao.AccountInfoDao.addTry      : <==    Updates: 1
2020-03-10 12:38:09.593  INFO 23404 --- [nio-9903-exec-6] c.t.h.s.impl.AccountInfoServiceImpl      : bank1 try end 结束执行...xid:1237236275664916480
2020-03-10 12:38:09.599 DEBUG 23404 --- [nDataHandler-27] o.d.h.c.s.e.HmilyTransactionExecutor     : hmily transaction confirm .......!start
2020-03-10 12:38:09.600  INFO 23404 --- [nDataHandler-27] c.t.h.s.impl.AccountInfoServiceImpl      : bank1 confirm begin 开始执行...xid:1237236275664916480,accountNo:1,amount:100.0
2020-03-10 12:38:13.389  INFO 23404 --- [elf-recovery-11] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30
2020-03-10 12:38:31.350  INFO 23404 --- [self-recovery-1] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30
2020-03-10 12:38:31.880  INFO 23404 --- [elf-recovery-22] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30
2020-03-10 12:38:43.391  INFO 23404 --- [elf-recovery-11] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30

 

2020-03-10 12:38:09.578 DEBUG 23320 --- [nio-9904-exec-9] o.d.h.c.s.e.HmilyTransactionExecutor     : participant hmily transaction start..:HmilyTransactionContext(transId=1237236275664916480, action=1, role=1)
2020-03-10 12:38:09.580  INFO 23320 --- [nio-9904-exec-9] c.t.h.s.impl.AccountInfoServiceImpl      : bank2 try begin 开始执行...xid:1237236275664916480
2020-03-10 12:38:09.617 DEBUG 23320 --- [io-9904-exec-10] o.d.h.c.s.e.HmilyTransactionExecutor     : hmily transaction confirm .......!start
2020-03-10 12:38:09.621  INFO 23320 --- [io-9904-exec-10] c.t.h.s.impl.AccountInfoServiceImpl      : bank2 confirm begin 开始执行...xid:1237236275664916480
2020-03-10 12:38:09.625 DEBUG 23320 --- [io-9904-exec-10] c.t.h.dao.AccountInfoDao.isExistConfirm  : ==>  Preparing: select count(1) from local_confirm_log where tx_no = ? 
2020-03-10 12:38:09.627 DEBUG 23320 --- [io-9904-exec-10] c.t.h.dao.AccountInfoDao.isExistConfirm  : ==> Parameters: 1237236275664916480(String)
2020-03-10 12:38:09.631 DEBUG 23320 --- [io-9904-exec-10] c.t.h.dao.AccountInfoDao.isExistConfirm  : <==      Total: 1
2020-03-10 12:38:09.632 DEBUG 23320 --- [io-9904-exec-10] c.t.h.d.A.addAccountBalance              : ==>  Preparing: update account_info set account_balance=account_balance + ? where account_no=? 
2020-03-10 12:38:09.632 DEBUG 23320 --- [io-9904-exec-10] c.t.h.d.A.addAccountBalance              : ==> Parameters: 100.0(Double), 2(String)
2020-03-10 12:38:09.634 DEBUG 23320 --- [io-9904-exec-10] c.t.h.d.A.addAccountBalance              : <==    Updates: 1
2020-03-10 12:38:09.634 DEBUG 23320 --- [io-9904-exec-10] c.t.hmily.dao.AccountInfoDao.addConfirm  : ==>  Preparing: insert into local_confirm_log values(?,now()); 
2020-03-10 12:38:09.635 DEBUG 23320 --- [io-9904-exec-10] c.t.hmily.dao.AccountInfoDao.addConfirm  : ==> Parameters: 1237236275664916480(String)
2020-03-10 12:38:09.637 DEBUG 23320 --- [io-9904-exec-10] c.t.hmily.dao.AccountInfoDao.addConfirm  : <==    Updates: 1
2020-03-10 12:38:09.637  INFO 23320 --- [io-9904-exec-10] c.t.h.s.impl.AccountInfoServiceImpl      : bank2 confirm end 结束执行...xid:1237236275664916480

 

转账失败的时候:

技术图片

 

 

2020-03-10 12:40:01.361  INFO 23404 --- [self-recovery-1] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30
2020-03-10 12:40:01.887  INFO 23404 --- [elf-recovery-22] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30
2020-03-10 12:40:02.294 DEBUG 23404 --- [nio-9903-exec-9] o.d.h.c.s.e.HmilyTransactionExecutor     : ......hmily transaction starter....
2020-03-10 12:40:02.295  INFO 23404 --- [nio-9903-exec-9] c.t.h.s.impl.AccountInfoServiceImpl      : bank1 try begin 开始执行...xid:1237236748702715904
2020-03-10 12:40:02.295 DEBUG 23404 --- [nio-9903-exec-9] c.t.hmily.dao.AccountInfoDao.isExistTry  : ==>  Preparing: select count(1) from local_try_log where tx_no = ? 
2020-03-10 12:40:02.295 DEBUG 23404 --- [nio-9903-exec-9] c.t.hmily.dao.AccountInfoDao.isExistTry  : ==> Parameters: 1237236748702715904(String)
2020-03-10 12:40:02.297 DEBUG 23404 --- [nio-9903-exec-9] c.t.hmily.dao.AccountInfoDao.isExistTry  : <==      Total: 1
2020-03-10 12:40:02.298 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.dao.AccountInfoDao.isExistConfirm  : ==>  Preparing: select count(1) from local_confirm_log where tx_no = ? 
2020-03-10 12:40:02.299 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.dao.AccountInfoDao.isExistConfirm  : ==> Parameters: 1237236748702715904(String)
2020-03-10 12:40:02.303 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.dao.AccountInfoDao.isExistConfirm  : <==      Total: 1
2020-03-10 12:40:02.304 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.dao.AccountInfoDao.isExistCancel   : ==>  Preparing: select count(1) from local_cancel_log where tx_no = ? 
2020-03-10 12:40:02.304 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.dao.AccountInfoDao.isExistCancel   : ==> Parameters: 1237236748702715904(String)
2020-03-10 12:40:02.305 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.dao.AccountInfoDao.isExistCancel   : <==      Total: 1
2020-03-10 12:40:02.306 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.d.A.subtractAccountBalance         : ==>  Preparing: update account_info set account_balance=account_balance - ? where account_balance>=? and account_no=? 
2020-03-10 12:40:02.306 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.d.A.subtractAccountBalance         : ==> Parameters: 2.0(Double), 2.0(Double), 1(String)
2020-03-10 12:40:02.308 DEBUG 23404 --- [nio-9903-exec-9] c.t.h.d.A.subtractAccountBalance         : <==    Updates: 1
2020-03-10 12:40:02.309 DEBUG 23404 --- [nio-9903-exec-9] c.t.hmily.dao.AccountInfoDao.addTry      : ==>  Preparing: insert into local_try_log values(?,now()); 
2020-03-10 12:40:02.309 DEBUG 23404 --- [nio-9903-exec-9] c.t.hmily.dao.AccountInfoDao.addTry      : ==> Parameters: 1237236748702715904(String)
2020-03-10 12:40:02.310 DEBUG 23404 --- [nio-9903-exec-9] c.t.hmily.dao.AccountInfoDao.addTry      : <==    Updates: 1
2020-03-10 12:40:02.326 DEBUG 23404 --- [nDataHandler-28] o.d.h.c.s.e.HmilyTransactionExecutor     : tcc cancel ...........start!
2020-03-10 12:40:02.327  INFO 23404 --- [nDataHandler-28] c.t.h.s.impl.AccountInfoServiceImpl      : bank1 cancel begin 开始执行...xid:1237236748702715904
2020-03-10 12:40:02.327 DEBUG 23404 --- [nDataHandler-28] c.t.h.dao.AccountInfoDao.isExistCancel   : ==>  Preparing: select count(1) from local_cancel_log where tx_no = ? 
2020-03-10 12:40:02.328 DEBUG 23404 --- [nDataHandler-28] c.t.h.dao.AccountInfoDao.isExistCancel   : ==> Parameters: 1237236748702715904(String)
2020-03-10 12:40:02.329 DEBUG 23404 --- [nDataHandler-28] c.t.h.dao.AccountInfoDao.isExistCancel   : <==      Total: 1
2020-03-10 12:40:02.329 DEBUG 23404 --- [nDataHandler-28] c.t.hmily.dao.AccountInfoDao.isExistTry  : ==>  Preparing: select count(1) from local_try_log where tx_no = ? 
2020-03-10 12:40:02.329 DEBUG 23404 --- [nDataHandler-28] c.t.hmily.dao.AccountInfoDao.isExistTry  : ==> Parameters: 1237236748702715904(String)
2020-03-10 12:40:02.330 DEBUG 23404 --- [nDataHandler-28] c.t.hmily.dao.AccountInfoDao.isExistTry  : <==      Total: 1
2020-03-10 12:40:02.330  INFO 23404 --- [nDataHandler-28] c.t.h.s.impl.AccountInfoServiceImpl      : bank1 空回滚处理,try没有执行,不允许cancel执行,xid:1237236748702715904
2020-03-10 12:40:02.334 ERROR 23404 --- [nio-9903-exec-9] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/bank1] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: 人为制造异常,xid:{}1237236748702715904] with root cause

java.lang.RuntimeException: 人为制造异常,xid:{}1237236748702715904
    at com.topcheer.hmily.service.impl.AccountInfoServiceImpl.updateAccountBalance(AccountInfoServiceImpl.java:70) ~[classes/:na]
    at com.topcheer.hmily.service.impl.AccountInfoServiceImpl$$FastClassBySpringCGLIB$$5df86849.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.dromara.hmily.core.service.handler.StarterHmilyTransactionHandler.handler(StarterHmilyTransactionHandler.java:70) ~[hmily-core-2.0.4-RELEASE.jar:2.0.4-RELEASE]
    at org.dromara.hmily.core.service.impl.HmilyTransactionAspectServiceImpl.invoke(HmilyTransactionAspectServiceImpl.java:63) ~[hmily-core-2.0.4-RELEASE.jar:2.0.4-RELEASE]
    at org.dromara.hmily.springcloud.interceptor.SpringCloudHmilyTransactionInterceptor.interceptor(SpringCloudHmilyTransactionInterceptor.java:78) ~[hmily-springcloud-2.0.4-RELEASE.jar:2.0.4-RELEASE]
    at org.dromara.hmily.core.interceptor.AbstractHmilyTransactionAspect.interceptTccMethod(AbstractHmilyTransactionAspect.java:61) ~[hmily-core-2.0.4-RELEASE.jar:2.0.4-RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at com.topcheer.hmily.service.impl.AccountInfoServiceImpl$$EnhancerBySpringCGLIB$$e5c6bb3b.updateAccountBalance(<generated>) ~[classes/:na]
    at com.topcheer.hmily.controller.Bank1Controller.transfer(Bank1Controller.java:20) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90) ~[spring-boot-actuator-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117) ~[spring-boot-actuator-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106) ~[spring-boot-actuator-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

2020-03-10 12:40:13.398  INFO 23404 --- [elf-recovery-11] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30
2020-03-10 12:40:02.318 DEBUG 23320 --- [nio-9904-exec-2] o.d.h.c.s.e.HmilyTransactionExecutor     : participant hmily transaction start..:HmilyTransactionContext(transId=1237236748702715904, action=1, role=1)
2020-03-10 12:40:02.318  INFO 23320 --- [nio-9904-exec-2] c.t.h.s.impl.AccountInfoServiceImpl      : bank2 try begin 开始执行...xid:1237236748702715904
2020-03-10 12:40:02.343 DEBUG 23320 --- [nio-9904-exec-3] o.d.h.c.s.e.HmilyTransactionExecutor     : tcc cancel ...........start!
2020-03-10 12:40:02.343  INFO 23320 --- [nio-9904-exec-3] c.t.h.s.impl.AccountInfoServiceImpl      : bank2 cancel begin 开始执行...xid:1237236748702715904
2020-03-10 12:40:22.229  INFO 23320 --- [self-recovery-2] .s.HmilyTransactionSelfRecoveryScheduled : self recovery execute delayTime:30

如果拿TCC事务的处理流程与2PC两阶段提交做比较,2PC通常都是在跨库的DB层面,而TCC则在应用层面的处
理,需要通过业务逻辑来实现。这种分布式事务的实现方式的优势在于,可以让应用自己定义数据操作的粒度,使
得降低锁冲突、提高吞吐量成为可能。
而不足之处则在于对应用的侵入性非常强,业务逻辑的每个分支都需要实现try、confirm、cancel三个操作。此
外,其实现难度也比较大,需要按照网络状态、系统故障等不同的失败原因实现不同的回滚策略。

以上是关于分布式事务解决方案之TCC的主要内容,如果未能解决你的问题,请参考以下文章

详解分布式事务解决方案之TCC机制

[分布式事务-TCC] 6. TCC的优化方案之三:二阶段异步化

[分布式事务-TCC] 6. TCC的优化方案之三:二阶段异步化

[分布式事务-TCC] 6. TCC的优化方案之三:二阶段异步化

分布式事务之TCC事务模型

让您轻松理解分布式事务解决方案之TCC