
与AT逻辑相同 只是不依赖关系型数据库的事务支持 不用快照 效率提升
TCC的空回滚与悬挂:
当开启全局事务时
B分支因某种原因阻塞 全局事务管理者因等待时间过长 会开启全局回滚
B分因没有进行try操作 而只能进行空操作 此为空回滚
所有在cancel操作之前要进行try操作的判断 如果没有进行过try操作 就要手动进行空回滚
而当全局已经进行回滚时 阻塞的B回滚这时进行了try操作 但这时全局事务已经完成
B分支就被挂起
为预防这种被挂起的情况
在try操作前 判断cancel有没有执行过 若执行过了 说明被空回滚过 就不去执行try
TCC的实现:
先定义一张表 记录分支事务的判断 冻结的金额
CREATE TABLE `account_freeze_tbl` (`xid` varchar(128) NOT NULL,`user_id` varchar(255) DEFAULT NULL COMMENT '用户id',`freeze_money` int(11) unsigned DEFAULT '0' COMMENT '冻结金额',`state` int(1) DEFAULT NULL COMMENT '事务状态,0:try,1:confirm,2:cancel',PRIMARY KEY (`xid`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

在需要使用TCC的微服务里 定义TCC接口 里面三个方法 分别就是TCC方法
try方法 的userid和monet可以放在上下文中 通过@BusinessActionContextParameter注解放入 自动就注入到上下文当中了
@LocalTCC // 设置TCC模式public interface TCCAccountService {/*** 从用户账户中扣款*/// 此注解使用在try方法上,方法名称可以自定义// 预留冻结@TwoPhaseBusinessAction(name = "deduct",commitMethod = "confirm",rollbackMethod = "cancel")void deduct(@BusinessActionContextParameter(paramName = "userId") String userId,@BusinessActionContextParameter(paramName = "money") int money);// confirm: 提交// BusinessActionContext: 业务上下文对象void confirm(BusinessActionContext context);// cancel: 事务补偿void cancel(BusinessActionContext context);}
编写实现类
@Servicepublic class TCCAccountServiceImpl implements TCCAccountService {@Autowiredprivate AccountMapper accountMapper;@Autowiredprivate AccountFreezeMapper accountFreezeMapper;@Overridepublic void deduct(String userId, int money) {String xid = RootContext.getXID();// try业务中,根据xid查询account_freeze ,如果已经存在则证明Cancel已经执行,拒绝执行try业务AccountFreeze freeze = accountFreezeMapper.selectById(xid);if (!(ObjectUtils.isEmpty(freeze))) {// 优化方案accountFreezeMapper.deleteById(xid);return;}//- 记录冻结金额和事务状态到account_freeze表AccountFreeze accountFreeze = new AccountFreeze();accountFreeze.setXid(xid);accountFreeze.setFreezeMoney(money);accountFreeze.setUserId(userId);accountFreeze.setState(AccountFreeze.State.TRY);accountFreezeMapper.insert(accountFreeze);// - 扣减account表可用金额accountMapper.deduct(userId, money);}@Overridepublic void confirm(BusinessActionContext context) {// 根据xid删除account_freeze表的冻结记录String xid = context.getXid();accountFreezeMapper.deleteById(xid);}@Overridepublic void cancel(BusinessActionContext context) {String xid = context.getXid();String userId = context.getActionContext("userId").toString();Integer money = (Integer) context.getActionContext("money");// cancel业务中,根据xid查询account_freeze,如果为null则说明try还没做,需要空回滚AccountFreeze freeze = accountFreezeMapper.selectById(xid);if (ObjectUtils.isEmpty(freeze)) {AccountFreeze accountFreeze = new AccountFreeze();accountFreeze.setXid(xid);accountFreeze.setFreezeMoney(0);accountFreeze.setUserId(userId);accountFreeze.setState(AccountFreeze.State.CANCEL);accountFreezeMapper.insert(accountFreeze);return;}//- 修改account_freeze表,冻结金额为0,state为2AccountFreeze accountFreeze = new AccountFreeze();accountFreeze.setXid(xid);accountFreeze.setFreezeMoney(0);accountFreeze.setState(AccountFreeze.State.CANCEL);accountFreezeMapper.updateById(accountFreeze);// - 修改account表,恢复可用金额accountMapper.refund(userId, money);}}
