in integration/sofa-rpc/src/main/java/org/apache/seata/integration/sofa/rpc/TransactionContextProviderFilter.java [48:105]
public SofaResponse invoke(FilterInvoker filterInvoker, SofaRequest sofaRequest) throws SofaRpcException {
String xid = RootContext.getXID();
String rpcXid = getRpcXid(sofaRequest);
BranchType branchType = RootContext.getBranchType();
String rpcBranchType = getBranchType(sofaRequest);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("context in RootContext[{},{}], context in RpcContext[{},{}]", xid, branchType, rpcXid, rpcBranchType);
}
boolean bind = false;
if (xid != null) {
RpcInternalContext.getContext().setAttachment(RootContext.HIDDEN_KEY_XID, xid);
RpcInternalContext.getContext().setAttachment(RootContext.HIDDEN_KEY_BRANCH_TYPE, branchType.name());
} else {
if (null != rpcXid) {
RootContext.bind(rpcXid);
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) {
RootContext.bindBranchType(BranchType.TCC);
}
bind = true;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("bind[{}] to RootContext",rpcXid);
}
}
}
try {
return filterInvoker.invoke(sofaRequest);
} finally {
if (xid != null) {
RpcInternalContext.getContext().removeAttachment(RootContext.HIDDEN_KEY_XID);
RpcInternalContext.getContext().removeAttachment(RootContext.HIDDEN_KEY_BRANCH_TYPE);
}
if (bind) {
String unbindXid = RootContext.unbind();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("unbind[{}] from RootContext",unbindXid);
}
BranchType previousBranchType = RootContext.getBranchType();
if (BranchType.TCC == previousBranchType) {
RootContext.unbindBranchType();
}
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("xid in change during RPC from [{}] to [{}]", rpcXid,unbindXid);
}
if (unbindXid != null) {
RootContext.bind(unbindXid);
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("bind [{}] back to RootContext",unbindXid);
}
if (BranchType.TCC == previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
LOGGER.warn("bind branchType [{}] back to RootContext", previousBranchType);
}
}
}
}
}
}