image.png
与AT逻辑相同 只是不依赖关系型数据库的事务支持 不用快照 效率提升

TCC的空回滚与悬挂:

当开启全局事务时
B分支因某种原因阻塞 全局事务管理者因等待时间过长 会开启全局回滚
B分因没有进行try操作 而只能进行空操作 此为空回滚
所有在cancel操作之前要进行try操作的判断 如果没有进行过try操作 就要手动进行空回滚

而当全局已经进行回滚时 阻塞的B回滚这时进行了try操作 但这时全局事务已经完成
B分支就被挂起
为预防这种被挂起的情况
在try操作前 判断cancel有没有执行过 若执行过了 说明被空回滚过 就不去执行try

TCC的实现:

先定义一张表 记录分支事务的判断 冻结的金额

  1. CREATE TABLE `account_freeze_tbl` (
  2. `xid` varchar(128) NOT NULL,
  3. `user_id` varchar(255) DEFAULT NULL COMMENT '用户id',
  4. `freeze_money` int(11) unsigned DEFAULT '0' COMMENT '冻结金额',
  5. `state` int(1) DEFAULT NULL COMMENT '事务状态,0:try,1:confirm,2:cancel',
  6. PRIMARY KEY (`xid`) USING BTREE
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

image.png

在需要使用TCC的微服务里 定义TCC接口 里面三个方法 分别就是TCC方法
try方法 的userid和monet可以放在上下文中 通过@BusinessActionContextParameter注解放入 自动就注入到上下文当中了

  1. @LocalTCC // 设置TCC模式
  2. public interface TCCAccountService {
  3. /**
  4. * 从用户账户中扣款
  5. */
  6. // 此注解使用在try方法上,方法名称可以自定义
  7. // 预留冻结
  8. @TwoPhaseBusinessAction(name = "deduct",commitMethod = "confirm",rollbackMethod = "cancel")
  9. void deduct(
  10. @BusinessActionContextParameter(paramName = "userId") String userId,
  11. @BusinessActionContextParameter(paramName = "money") int money);
  12. // confirm: 提交
  13. // BusinessActionContext: 业务上下文对象
  14. void confirm(BusinessActionContext context);
  15. // cancel: 事务补偿
  16. void cancel(BusinessActionContext context);
  17. }

编写实现类

  1. @Service
  2. public class TCCAccountServiceImpl implements TCCAccountService {
  3. @Autowired
  4. private AccountMapper accountMapper;
  5. @Autowired
  6. private AccountFreezeMapper accountFreezeMapper;
  7. @Override
  8. public void deduct(String userId, int money) {
  9. String xid = RootContext.getXID();
  10. // try业务中,根据xid查询account_freeze ,如果已经存在则证明Cancel已经执行,拒绝执行try业务
  11. AccountFreeze freeze = accountFreezeMapper.selectById(xid);
  12. if (!(ObjectUtils.isEmpty(freeze))) {
  13. // 优化方案
  14. accountFreezeMapper.deleteById(xid);
  15. return;
  16. }
  17. //- 记录冻结金额和事务状态到account_freeze表
  18. AccountFreeze accountFreeze = new AccountFreeze();
  19. accountFreeze.setXid(xid);
  20. accountFreeze.setFreezeMoney(money);
  21. accountFreeze.setUserId(userId);
  22. accountFreeze.setState(AccountFreeze.State.TRY);
  23. accountFreezeMapper.insert(accountFreeze);
  24. // - 扣减account表可用金额
  25. accountMapper.deduct(userId, money);
  26. }
  27. @Override
  28. public void confirm(BusinessActionContext context) {
  29. // 根据xid删除account_freeze表的冻结记录
  30. String xid = context.getXid();
  31. accountFreezeMapper.deleteById(xid);
  32. }
  33. @Override
  34. public void cancel(BusinessActionContext context) {
  35. String xid = context.getXid();
  36. String userId = context.getActionContext("userId").toString();
  37. Integer money = (Integer) context.getActionContext("money");
  38. // cancel业务中,根据xid查询account_freeze,如果为null则说明try还没做,需要空回滚
  39. AccountFreeze freeze = accountFreezeMapper.selectById(xid);
  40. if (ObjectUtils.isEmpty(freeze)) {
  41. AccountFreeze accountFreeze = new AccountFreeze();
  42. accountFreeze.setXid(xid);
  43. accountFreeze.setFreezeMoney(0);
  44. accountFreeze.setUserId(userId);
  45. accountFreeze.setState(AccountFreeze.State.CANCEL);
  46. accountFreezeMapper.insert(accountFreeze);
  47. return;
  48. }
  49. //- 修改account_freeze表,冻结金额为0,state为2
  50. AccountFreeze accountFreeze = new AccountFreeze();
  51. accountFreeze.setXid(xid);
  52. accountFreeze.setFreezeMoney(0);
  53. accountFreeze.setState(AccountFreeze.State.CANCEL);
  54. accountFreezeMapper.updateById(accountFreeze);
  55. // - 修改account表,恢复可用金额
  56. accountMapper.refund(userId, money);
  57. }
  58. }