TM相当于一个中间商,是没有涉及到任何数据库底层操作的。
TransactionalTemplate
1、TM向TC端发起一次开启全局事务的请求
io.seata.tm.api.TransactionalTemplate#beginTransaction
-->
io.seata.tm.api.DefaultGlobalTransaction#begin(int, java.lang.String)
-->
io.seata.tm.DefaultTransactionManager#begin
@Override
public String begin(String applicationId, String transactionServiceGroup, String name, int timeout)throws TransactionException {GlobalBeginRequest request = new GlobalBeginRequest();request.setTransactionName(name);request.setTimeout(timeout);GlobalBeginResponse response = (GlobalBeginResponse) syncCall(request);if (response.getResultCode() == ResultCode.Failed) {throw new TmTransactionException(TransactionExceptionCode.BeginFailed, response.getMsg());}return response.getXid();
}
2、 TM向TC端发起一次全局提交事务请求
io.seata.tm.api.TransactionalTemplate#commitTransaction
-->
io.seata.tm.DefaultTransactionManager#commit
@Override
public GlobalStatus commit(String xid) throws TransactionException {GlobalCommitRequest globalCommit = new GlobalCommitRequest();globalCommit.setXid(xid);GlobalCommitResponse response = (GlobalCommitResponse) syncCall(globalCommit);return response.getGlobalStatus();
}
3、TM向TC端发起一次全局回滚事务请求
io.seata.tm.api.TransactionalTemplate#rollbackTransaction
io.seata.tm.DefaultTransactionManager#rollback@Overridepublic GlobalStatus rollback(String xid) throws TransactionException {GlobalRollbackRequest globalRollback = new GlobalRollbackRequest();globalRollback.setXid(xid);GlobalRollbackResponse response = (GlobalRollbackResponse) syncCall(globalRollback);return response.getGlobalStatus();}