Java Code Examples for io.seata.core.context.RootContext#getXID()
The following examples show how to use
io.seata.core.context.RootContext#getXID() .
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: OrderService.java From seata-samples with Apache License 2.0 | 6 votes |
public void create(String userId, String commodityCode, Integer count) { String xid = RootContext.getXID(); LOGGER.info("create order in transaction: " + xid); // 定单总价 = 订购数量(count) * 商品单价(100) int orderMoney = count * 100; // 生成订单 jdbcTemplate.update("insert order_tbl(user_id,commodity_code,count,money) values(?,?,?,?)", new Object[] {userId, commodityCode, count, orderMoney}); // 调用账户余额扣减 String result = accountFeignClient.reduce(userId, orderMoney); if (!SUCCESS.equals(result)) { throw new RuntimeException("Failed to call Account Service. "); } }
Example 2
Source File: SeataFeignClient.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
private Request getModifyRequest(Request request) { String xid = RootContext.getXID(); if (StringUtils.isEmpty(xid)) { return request; } Map<String, Collection<String>> headers = new HashMap<>(MAP_SIZE); headers.putAll(request.headers()); List<String> seataXid = new ArrayList<>(); seataXid.add(xid); headers.put(RootContext.KEY_XID, seataXid); return Request.create(request.method(), request.url(), headers, request.body(), request.charset()); }
Example 3
Source File: BusinessService.java From seata-samples with Apache License 2.0 | 6 votes |
@GlobalTransactional public void purchase(String userId, String commodityCode, int orderCount, boolean rollback) { String xid = RootContext.getXID(); LOGGER.info("New Transaction Begins: " + xid); String result = storageFeignClient.deduct(commodityCode, orderCount); if (!SUCCESS.equals(result)) { throw new RuntimeException("库存服务调用失败,事务回滚!"); } result = orderFeignClient.create(userId, commodityCode, orderCount); if (!SUCCESS.equals(result)) { throw new RuntimeException("订单服务调用失败,事务回滚!"); } if (rollback) { throw new RuntimeException("Force rollback ... "); } }
Example 4
Source File: TccTransactionService.java From seata-samples with Apache License 2.0 | 6 votes |
/** * 发起分布式事务 * * @return string string */ @GlobalTransactional public String doTransactionCommit(){ //第一个TCC 事务参与者 boolean result = tccActionOne.prepare(null, 1); if(!result){ throw new RuntimeException("TccActionOne failed."); } List list = new ArrayList(); list.add("c1"); list.add("c2"); result = tccActionTwo.prepare(null, "two", list); if(!result){ throw new RuntimeException("TccActionTwo failed."); } return RootContext.getXID(); }
Example 5
Source File: TccTransactionService.java From seata-samples with Apache License 2.0 | 6 votes |
/** * 发起分布式事务 * * @return string string */ @GlobalTransactional public String doTransactionCommit() { //第一个TCC 事务参与者 boolean result = tccActionOne.prepare(null, 1); if (!result) { throw new RuntimeException("TccActionOne failed."); } List list = new ArrayList(); list.add("c1"); list.add("c2"); result = tccActionTwo.prepare(null, "two", list); if (!result) { throw new RuntimeException("TccActionTwo failed."); } return RootContext.getXID(); }
Example 6
Source File: ActivityServiceImpl.java From seata-samples with Apache License 2.0 | 6 votes |
@GlobalTransactional public String doActivity(boolean commit){ //第一个TCC 事务参与者 boolean result = actionOne.prepare(null, 1); if(!commit || !result){ throw new RuntimeException("TccActionOne failed."); } //第二个TCC 事务参与者 List list = new ArrayList(); list.add("c1"); list.add("c2"); result = actionTwo.prepare(null, "two", list); if(!result){ throw new RuntimeException("TccActionTwo failed."); } return RootContext.getXID(); }
Example 7
Source File: SeataHandlerInterceptor.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
@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 8
Source File: TransactionPropagationFilter.java From jboot with Apache License 2.0 | 5 votes |
@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 9
Source File: SeataRestTemplateInterceptor.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest); String xid = RootContext.getXID(); if (!StringUtils.isEmpty(xid)) { requestWrapper.getHeaders().add(RootContext.KEY_XID, xid); } return clientHttpRequestExecution.execute(requestWrapper, bytes); }
Example 10
Source File: SeataInterceptor.java From x7 with Apache License 2.0 | 5 votes |
@Override public KV apply(SimpleRestTemplate template) { String xid = RootContext.getXID(); if (xid != null && !xid.trim().equals("")) { return template.header(RootContext.KEY_XID, xid); } return null; }
Example 11
Source File: SeataXidFilter.java From demo-seata-springcloud with Apache License 2.0 | 5 votes |
@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 12
Source File: SeataXidFilter.java From demo-seata-springcloud with Apache License 2.0 | 5 votes |
@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 13
Source File: RequestHeaderInterceptor.java From demo-seata-springcloud with Apache License 2.0 | 5 votes |
@Override public void apply(RequestTemplate template) { String xid = RootContext.getXID(); if (StringUtils.isNotBlank(xid)) { template.header(SeataConstant.XID_HEADER, xid); } }
Example 14
Source File: SeataXidFilter.java From demo-seata-springcloud with Apache License 2.0 | 5 votes |
@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 15
Source File: SeataRestTemplateInterceptor.java From seata-samples with Apache License 2.0 | 5 votes |
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest); String xid = RootContext.getXID(); if (StringUtils.isNotEmpty(xid)) { requestWrapper.getHeaders().add(RootContext.KEY_XID, xid); } return clientHttpRequestExecution.execute(requestWrapper, bytes); }
Example 16
Source File: TccTransactionService.java From seata-samples with Apache License 2.0 | 5 votes |
/** * 发起分布式事务 * * @return string string */ @GlobalTransactional public String doTransactionCommit(){ //第一个TCC 事务参与者 boolean result = tccActionOne.prepare(null, 1); if(!result){ throw new RuntimeException("TccActionOne failed."); } result = tccActionTwo.prepare(null, "two"); if(!result){ throw new RuntimeException("TccActionTwo failed."); } return RootContext.getXID(); }
Example 17
Source File: TccTransactionService.java From seata-samples with Apache License 2.0 | 5 votes |
/** * 发起分布式事务 * * @return string string */ @GlobalTransactional public String doTransactionCommit(){ //第一个TCC 事务参与者 boolean result = tccActionOne.prepare(null, 1); if(!result){ throw new RuntimeException("TccActionOne failed."); } result = tccActionTwo.prepare(null, "two"); if(!result){ throw new RuntimeException("TccActionTwo failed."); } return RootContext.getXID(); }
Example 18
Source File: AccountService.java From seata-samples with Apache License 2.0 | 5 votes |
@Transactional public void reduce(String userId, int money) { String xid = RootContext.getXID(); LOGGER.info("reduce account balance in transaction: " + xid); jdbcTemplate.update("update account_tbl set money = money - ? where user_id = ?", new Object[] {money, userId}); int balance = jdbcTemplate.queryForObject("select money from account_tbl where user_id = ?", new Object[] {userId}, Integer.class); LOGGER.info("balance after transaction: " + balance); if (balance < 0) { throw new RuntimeException("Not Enough Money ..."); } }
Example 19
Source File: SeataHystrixConcurrencyStrategy.java From spring-cloud-alibaba with Apache License 2.0 | 4 votes |
SeataContextCallable(Callable<K> actual, RequestAttributes requestAttribute) { this.actual = actual; this.requestAttributes = requestAttribute; this.xid = RootContext.getXID(); }
Example 20
Source File: SeataATShardingTransactionManager.java From shardingsphere with Apache License 2.0 | 4 votes |
@Override public boolean isInTransaction() { Preconditions.checkState(enableSeataAT, "sharding seata-at transaction has been disabled."); return null != RootContext.getXID(); }