at模式讲解
public Object execute(TransactionalExecutor business) throws Throwable {
// 1 get transactionInfo 获取事务相关的配置信息
// 1.1 get or create a transaction 获取事务
// 1.2 根据事务不同的传播机制 进行处理
try {
// 2. begin transaction
beginTransaction(txInfo, tx);
Object rs = null;
try {
// Do Your Business
rs = business.execute();
} catch (Throwable ex) {
// 3.the needed business exception to rollback.
completeTransactionAfterThrowing(txInfo, tx, ex);
throw ex;
}
// 4. everything is fine, commit.
commitTransaction(tx);
return rs;
} finally {
//5. clear
triggerAfterCompletion();
cleanUp();
}
} finally {
tx.resume(suspendedResourcesHolder);
}
}
假设现在有一个上游方法,内部调用了a服务和b服务,这两个服务都是分布式的,并且都更新了各自的数据,则相应的时序逻辑如下:
image.png