优化seata测试案例,使全局事务可以 回滚

pull/5432/head
kezhijie 2023-09-26 10:17:14 +08:00
parent ffeb607ad3
commit 78e371ab9f
4 changed files with 19 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package org.jeecg.modules.test.seata.account.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
@ -34,6 +35,7 @@ public class SeataAccountServiceImpl implements SeataAccountService {
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
public void reduceBalance(Long userId, BigDecimal amount) {
log.info("xid:"+ RootContext.getXID());
log.info("=============ACCOUNT START=================");
SeataAccount account = accountMapper.selectById(userId);
Assert.notNull(account, "用户不存在");

View File

@ -1,6 +1,8 @@
package org.jeecg.modules.test.seata.order.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
@ -39,6 +41,7 @@ public class SeataOrderServiceImpl implements SeataOrderService {
@Transactional(rollbackFor = Exception.class)
@GlobalTransactional
public void placeOrder(PlaceOrderRequest request) {
log.info("xid:"+RootContext.getXID());
log.info("=============ORDER START=================");
Long userId = request.getUserId();
Long productId = request.getProductId();
@ -58,7 +61,12 @@ public class SeataOrderServiceImpl implements SeataOrderService {
// 扣减库存并计算总价
BigDecimal amount = productClient.reduceStock(productId, count);
// 扣减余额
accountClient.reduceBalance(userId, amount);
String str = accountClient.reduceBalance(userId, amount);
// feign响应被二次封装判断使主事务回滚
JSONObject jsonObject = JSONObject.parseObject(str);
if (jsonObject.getInteger("code") != 200) {
throw new RuntimeException();
}
order.setStatus(OrderStatus.SUCCESS);
order.setTotalPrice(amount);

View File

@ -1,6 +1,7 @@
package org.jeecg.modules.test.seata.product.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
@ -35,6 +36,7 @@ public class SeataProductServiceImpl implements SeataProductService {
@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
@Override
public BigDecimal reduceStock(Long productId, Integer count) {
log.info("xid:"+ RootContext.getXID());
log.info("=============PRODUCT START=================");
// 检查库存
SeataProduct product = productMapper.selectById(productId);

View File

@ -20,6 +20,12 @@
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-boot-starter-cloud</artifactId>
<version>${jeecgboot.version}</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jeecgframework.boot</groupId>