judging xid status to avoid repeated processing
@@ -40,10 +40,15 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory
.getLogger(SeataHandlerInterceptor.class);
+ public static final String BIND_XID = "BIND_XID";
+
+ public static final String UNBIND_XID = "UNBIND_XID";
+
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) {
-
+ Object xid_status = request.getAttribute(BIND_XID);
+ if (null == xid_status || !(boolean)xid_status) {
String xid = RootContext.getXID();
String rpcXid = request.getHeader(RootContext.KEY_XID);
if (log.isDebugEnabled()) {
@@ -52,10 +57,12 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
if (xid == null && rpcXid != null) {
RootContext.bind(rpcXid);
+ request.setAttribute(BIND_XID, true);
if (log.isDebugEnabled()) {
log.debug("bind {} to RootContext", rpcXid);
}
}
+ }
return true;
}
@@ -69,6 +76,8 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
return;
}
+ Object xid_status = request.getAttribute(UNBIND_XID);
+ if (null == xid_status || !(boolean)xid_status) {
String unbindXid = RootContext.unbind();
if (log.isDebugEnabled()) {
log.debug("unbind {} from RootContext", unbindXid);
@@ -77,9 +86,11 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid);
if (unbindXid != null) {
RootContext.bind(unbindXid);
+ request.setAttribute(UNBIND_XID, true);
log.warn("bind {} back to RootContext", unbindXid);
}
}
}
+ }
}
