diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/web/SeataHandlerInterceptor.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/web/SeataHandlerInterceptor.java index dcc0b28c0..86d331b05 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/web/SeataHandlerInterceptor.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/web/SeataHandlerInterceptor.java @@ -19,11 +19,11 @@ package com.alibaba.cloud.seata.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import io.seata.common.util.StringUtils; import io.seata.core.context.RootContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; /** @@ -41,45 +41,45 @@ public class SeataHandlerInterceptor implements HandlerInterceptor { .getLogger(SeataHandlerInterceptor.class); @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, - Object handler) { + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + String xid = RootContext.getXID(); + String rpcXid = request.getHeader(RootContext.KEY_XID); + if (log.isDebugEnabled()) { + log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid); + } - String xid = RootContext.getXID(); - String rpcXid = request.getHeader(RootContext.KEY_XID); - if (log.isDebugEnabled()) { - log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid); - } + if (StringUtils.isBlank(xid) && rpcXid != null) { + RootContext.bind(rpcXid); + if (log.isDebugEnabled()) { + log.debug("bind {} to RootContext", rpcXid); + } + } - if (xid == null && rpcXid != null) { - RootContext.bind(rpcXid); - if (log.isDebugEnabled()) { - log.debug("bind {} to RootContext", rpcXid); - } - } - return true; - } + return true; + } - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, - Object handler, Exception e) { - String rpcXid = request.getHeader(RootContext.KEY_XID); + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { + if (StringUtils.isNotBlank(RootContext.getXID())) { + String rpcXid = request.getHeader(RootContext.KEY_XID); - if (StringUtils.isEmpty(rpcXid)) { - return; - } + if (StringUtils.isEmpty(rpcXid)) { + return; + } - String unbindXid = RootContext.unbind(); - if (log.isDebugEnabled()) { - log.debug("unbind {} from RootContext", unbindXid); - } - if (!rpcXid.equalsIgnoreCase(unbindXid)) { - log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid); - if (unbindXid != null) { - RootContext.bind(unbindXid); - log.warn("bind {} back to RootContext", unbindXid); - } - } - } + String unbindXid = RootContext.unbind(); + if (log.isDebugEnabled()) { + log.debug("unbind {} from RootContext", unbindXid); + } + if (!rpcXid.equalsIgnoreCase(unbindXid)) { + log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid); + if (unbindXid != null) { + RootContext.bind(unbindXid); + log.warn("bind {} back to RootContext", unbindXid); + } + } + } + } }