java如何实现tcc
TCC模式简介
TCC(Try-Confirm-Cancel)是一种分布式事务解决方案,通过业务逻辑拆分实现最终一致性。核心分为三个阶段:
- Try:预留资源,完成业务检查(如冻结账户余额)。
- Confirm:确认执行业务操作(如扣减冻结金额)。
- Cancel:取消操作,释放预留资源(如解冻金额)。
Java实现TCC的关键步骤
设计TCC接口
为每个参与者(如订单服务、库存服务)定义TCC接口,包含try、confirm、cancel方法:
public interface InventoryTccService {
@Transactional
boolean tryReserve(Long productId, int quantity); // Try阶段:预留库存
boolean confirmReserve(Long productId, int quantity); // Confirm阶段:确认扣除
boolean cancelReserve(Long productId, int quantity); // Cancel阶段:释放预留
}
实现TCC参与者
使用Spring框架管理事务,确保每个阶段幂等:
@Service
public class InventoryTccServiceImpl implements InventoryTccService {
@Override
public boolean tryReserve(Long productId, int quantity) {
// 检查库存并预留资源,记录状态到日志表
return inventoryDao.reduceStock(productId, quantity) > 0;
}
@Override
public boolean confirmReserve(Long productId, int quantity) {
// 确认预留,删除日志记录(幂等)
return tccLogDao.deleteLog(productId, "RESERVED");
}
@Override
public boolean cancelReserve(Long productId, int quantity) {
// 取消预留,恢复库存(幂等)
return inventoryDao.addStock(productId, quantity) > 0;
}
}
协调器实现
通过事务日志或消息队列(如RocketMQ)协调TCC流程:
public class TccCoordinator {
public void executeTcc() {
try {
// 1. 调用所有参与者的try方法
orderTccService.tryCreateOrder();
inventoryTccService.tryReserve();
// 2. 全部成功则调用confirm
orderTccService.confirmCreateOrder();
inventoryTccService.confirmReserve();
} catch (Exception e) {
// 3. 任一失败则调用cancel回滚
orderTccService.cancelCreateOrder();
inventoryTccService.cancelReserve();
}
}
}
幂等性与重试机制
- 幂等控制:通过唯一事务ID或日志表避免重复处理。
- 重试策略:对失败的Confirm/Cancel阶段采用定时任务补偿。
框架推荐
- Seata:支持TCC模式,提供全局事务管理。
- Hmily:轻量级TCC框架,集成Spring Cloud。
示例Seata配置:

@GlobalTransactional
public void placeOrder() {
tccService1.tryMethod();
tccService2.tryMethod();
}
注意事项
- 超时处理:设置Try阶段超时时间,避免资源长期锁定。
- 日志持久化:记录事务状态,防止宕机后状态丢失。
- 隔离性:通过业务设计避免脏读(如预留资源不可见)。






