Java Code Examples for io.seata.core.context.RootContext#bind()

The following examples show how to use io.seata.core.context.RootContext#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SeataFilter.java    From seata-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;
    String xid = req.getHeader(RootContext.KEY_XID.toLowerCase());
    boolean isBind = false;
    if (StringUtils.isNotBlank(xid)) {
        RootContext.bind(xid);
        isBind = true;
    }
    try {
        filterChain.doFilter(servletRequest, servletResponse);
    } finally {
        if (isBind) {
            RootContext.unbind();
        }
    }
}
 
Example 2
Source File: SeataHandlerInterceptor.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
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);
	}

	if (xid == null && rpcXid != null) {
		RootContext.bind(rpcXid);
		if (log.isDebugEnabled()) {
			log.debug("bind {} to RootContext", rpcXid);
		}
	}
	return true;
}
 
Example 3
Source File: SeataHandlerInterceptor.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
		Object handler, Exception e) {

	String rpcXid = request.getHeader(RootContext.KEY_XID);

	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);
		}
	}
}
 
Example 4
Source File: SeataXidFilter.java    From demo-seata-springcloud with Apache License 2.0 5 votes vote down vote up
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {
	String xid = RootContext.getXID();
	String restXid = request.getHeader(SeataConstant.XID_HEADER);
	boolean bind = false;
	if (StringUtils.isBlank(xid) && StringUtils.isNotBlank(restXid)) {
		RootContext.bind(restXid);
		bind = true;
		if (logger.isDebugEnabled()) {
			logger.debug("bind[" + restXid + "] to RootContext");
		}
	}
	try {
		filterChain.doFilter(request, response);
	} finally {
		if (bind) {
			String unbindXid = RootContext.unbind();
			if (logger.isDebugEnabled()) {
				logger.debug("unbind[" + unbindXid + "] from RootContext");
			}
			if (!restXid.equalsIgnoreCase(unbindXid)) {
				logger.warn("xid in change during http rest from " + restXid + " to " + unbindXid);
				if (unbindXid != null) {
					RootContext.bind(unbindXid);
					logger.warn("bind [" + unbindXid + "] back to RootContext");
				}
			}
		}
	}

}
 
Example 5
Source File: SeataXidFilter.java    From demo-seata-springcloud with Apache License 2.0 5 votes vote down vote up
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {
	String xid = RootContext.getXID();
	String restXid = request.getHeader(SeataConstant.XID_HEADER);
	boolean bind = false;
	if (StringUtils.isBlank(xid) && StringUtils.isNotBlank(restXid)) {
		RootContext.bind(restXid);
		bind = true;
		if (logger.isDebugEnabled()) {
			logger.debug("bind[" + restXid + "] to RootContext");
		}
	}
	try {
		filterChain.doFilter(request, response);
	} finally {
		if (bind) {
			String unbindXid = RootContext.unbind();
			if (logger.isDebugEnabled()) {
				logger.debug("unbind[" + unbindXid + "] from RootContext");
			}
			if (!restXid.equalsIgnoreCase(unbindXid)) {
				logger.warn("xid in change during http rest from " + restXid + " to " + unbindXid);
				if (unbindXid != null) {
					RootContext.bind(unbindXid);
					logger.warn("bind [" + unbindXid + "] back to RootContext");
				}
			}
		}
	}

}
 
Example 6
Source File: SeataXidFilter.java    From demo-seata-springcloud with Apache License 2.0 5 votes vote down vote up
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {
	String xid = RootContext.getXID();
	String restXid = request.getHeader(SeataConstant.XID_HEADER);
	boolean bind = false;
	if (StringUtils.isBlank(xid) && StringUtils.isNotBlank(restXid)) {
		RootContext.bind(restXid);
		bind = true;
		if (logger.isDebugEnabled()) {
			logger.debug("bind[" + restXid + "] to RootContext");
		}
	}
	try {
		filterChain.doFilter(request, response);
	} finally {
		if (bind) {
			String unbindXid = RootContext.unbind();
			if (logger.isDebugEnabled()) {
				logger.debug("unbind[" + unbindXid + "] from RootContext");
			}
			if (!restXid.equalsIgnoreCase(unbindXid)) {
				logger.warn("xid in change during http rest from " + restXid + " to " + unbindXid);
				if (unbindXid != null) {
					RootContext.bind(unbindXid);
					logger.warn("bind [" + unbindXid + "] back to RootContext");
				}
			}
		}
	}

}
 
Example 7
Source File: AutoCpsLocalTransactionExecutor.java    From EasyTransaction with Apache License 2.0 5 votes vote down vote up
public static <R> R executeWithGlobalLockCheck(Callable<R> call) throws Exception {
    try {
        RootContext.bind("Local Tranaction with Global lock support");
        return call.call();
    } finally {
        RootContext.unbind();
    }
}
 
Example 8
Source File: AbstractAutoCpsMethod.java    From EasyTransaction with Apache License 2.0 5 votes vote down vote up
@Override
public final R doAutoCpsBusiness(P param) {

    TransactionId transactionId = MetaDataFilter.getMetaData(EasytransConstant.CallHeadKeys.PARENT_TRX_ID_KEY);
    String xid = getFescarXid(transactionId);

    try {
        RootContext.bind(xid);
        return doBusiness(param);
    } finally {
        RootContext.unbind();
    }
}
 
Example 9
Source File: SeataHystrixConcurrencyStrategy.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public K call() throws Exception {
	try {
		RequestContextHolder.setRequestAttributes(requestAttributes);
		RootContext.bind(xid);
		return actual.call();
	} finally {
		RootContext.unbind();
		RequestContextHolder.resetRequestAttributes();
	}
}
 
Example 10
Source File: TransactionPropagationFilter.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (!JbootSeataManager.me().isEnable()){
        return invoker.invoke(invocation);
    }
    String xid = RootContext.getXID();
    String rpcXid = RpcContext.getContext().getAttachment(RootContext.KEY_XID);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("xid in RootContext[" + xid + "] xid in RpcContext[" + rpcXid + "]");
    }
    boolean bind = false;
    if (xid != null) {
        RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid);
    } else {
        if (rpcXid != null) {
            RootContext.bind(rpcXid);
            bind = true;
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("bind[" + rpcXid + "] to RootContext");
            }
        }
    }
    try {
        return invoker.invoke(invocation);
    } finally {
        if (bind) {
            String unbindXid = RootContext.unbind();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("unbind[" + unbindXid + "] from RootContext");
            }
            if (!rpcXid.equalsIgnoreCase(unbindXid)) {
                LOGGER.warn("xid in change during RPC from " + rpcXid + " to " + unbindXid);
                if (unbindXid != null) {
                    RootContext.bind(unbindXid);
                    LOGGER.warn("bind [" + unbindXid + "] back to RootContext");
                }
            }
        }
    }
}
 
Example 11
Source File: TransactionalSQLExecutionHook.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Override
public void start(final String dataSourceName, final String sql, final List<Object> parameters,
                  final DataSourceMetaData dataSourceMetaData, final boolean isTrunkThread, final Map<String, Object> shardingExecuteDataMap) {
    if (isTrunkThread) {
        if (RootContext.inGlobalTransaction()) {
            ExecutorDataMap.getValue().put(SEATA_TX_XID, RootContext.getXID());
        }
    } else if (!RootContext.inGlobalTransaction() && shardingExecuteDataMap.containsKey(SEATA_TX_XID)) {
        RootContext.bind((String) shardingExecuteDataMap.get(SEATA_TX_XID));
        seataBranch = true;
    }
}
 
Example 12
Source File: SeataATShardingTransactionManagerTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@SneakyThrows(ReflectiveOperationException.class)
private void setXID(final String xid) {
    Field field = SeataTransactionHolder.get().getClass().getDeclaredField("xid");
    field.setAccessible(true);
    field.set(SeataTransactionHolder.get(), xid);
    RootContext.bind(xid);
}
 
Example 13
Source File: TransactionalSQLExecutionHookTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertTrunkThreadExecute() {
    RootContext.bind("xid");
    executionHook.start("ds", "SELECT 1", Collections.emptyList(), dataSourceMetaData, true, shardingExecuteDataMap);
    assertThat(ExecutorDataMap.getValue().get("SEATA_TX_XID"), is(RootContext.getXID()));
    executionHook.finishSuccess();
    assertTrue(RootContext.inGlobalTransaction());
}