use of com.baomidou.dynamic.datasource.annotation.DS in project platform-base by SummerWindL.
the class PostgreSqlNotifyService method jdbcPgNofifyTest.
@DS("slave_1")
public BasePostgreResponse jdbcPgNofifyTest() {
BasePostgreResponse resp = new BasePostgreResponse();
List<BasePostgreResponse> query = jdbcTemplate.query("select * from f_test_pg_notify('1')", new RowMapper<BasePostgreResponse>() {
@Override
public BasePostgreResponse mapRow(ResultSet resultSet, int i) throws SQLException {
resp.setRetcode(resultSet.getInt("retcode"));
resp.setRetvalue(resultSet.getString("retvalue"));
return resp;
}
});
return !CollectionUtils.isEmpty(query) ? query.get(0) : new BasePostgreResponse();
}
use of com.baomidou.dynamic.datasource.annotation.DS in project jeecg-boot by jeecgboot.
the class SeataAccountServiceImpl method reduceBalance.
/**
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
*/
@DS("account")
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void reduceBalance(Long userId, Double price) {
log.info("=============ACCOUNT START=================");
SeataAccount account = accountMapper.selectById(userId);
Assert.notNull(account, "用户不存在");
Double balance = account.getBalance();
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, price);
if (balance < price) {
log.warn("用户 {} 余额不足,当前余额:{}", userId, balance);
throw new RuntimeException("余额不足");
}
log.info("开始扣减用户 {} 余额", userId);
double currentBalance = account.getBalance() - price;
account.setBalance(currentBalance);
accountMapper.updateById(account);
log.info("扣减用户 {} 余额成功,扣减后用户账户余额为{}", userId, currentBalance);
log.info("=============ACCOUNT END=================");
}
use of com.baomidou.dynamic.datasource.annotation.DS in project jeecg-boot by jeecgboot.
the class SeataOrderServiceImpl method placeOrder.
@DS("order")
@Override
@Transactional
@GlobalTransactional
public void placeOrder(PlaceOrderRequest request) {
log.info("=============ORDER START=================");
Long userId = request.getUserId();
Long productId = request.getProductId();
Integer amount = request.getAmount();
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, amount);
SeataOrder order = SeataOrder.builder().userId(userId).productId(productId).status(OrderStatus.INIT).amount(amount).build();
orderMapper.insert(order);
log.info("订单一阶段生成,等待扣库存付款中");
// 扣减库存并计算总价
Double totalPrice = productService.reduceStock(productId, amount);
// 扣减余额
accountService.reduceBalance(userId, totalPrice);
order.setStatus(OrderStatus.SUCCESS);
order.setTotalPrice(totalPrice);
orderMapper.updateById(order);
log.info("订单已成功下单");
log.info("=============ORDER END=================");
}
use of com.baomidou.dynamic.datasource.annotation.DS in project jeecg-boot by jeecgboot.
the class SeataProductServiceImpl method reduceStock.
/**
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
*/
@DS("product")
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Double reduceStock(Long productId, Integer amount) {
log.info("=============PRODUCT START=================");
// 检查库存
SeataProduct product = productMapper.selectById(productId);
Assert.notNull(product, "商品不存在");
Integer stock = product.getStock();
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, amount);
if (stock < amount) {
log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
throw new RuntimeException("库存不足");
}
log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
// 扣减库存
int currentStock = stock - amount;
product.setStock(currentStock);
productMapper.updateById(product);
double totalPrice = product.getPrice() * amount;
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, amount, totalPrice);
log.info("=============PRODUCT END=================");
return totalPrice;
}
use of com.baomidou.dynamic.datasource.annotation.DS in project dynamic-datasource-samples by dynamic-datasource.
the class ProductServiceImpl method reduceStock.
/**
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
*/
@DS("product")
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Double reduceStock(Long productId, Integer amount) {
log.info("=============PRODUCT START=================");
log.info("当前 XID: {}", RootContext.getXID());
// 检查库存
Product product = productMapper.selectById(productId);
Assert.notNull(product, "商品不存在");
Integer stock = product.getStock();
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, amount);
if (stock < amount) {
log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
throw new RuntimeException("库存不足");
}
log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
// 扣减库存
int currentStock = stock - amount;
product.setStock(currentStock);
productMapper.updateById(product);
double totalPrice = product.getPrice() * amount;
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, amount, totalPrice);
log.info("=============PRODUCT END=================");
return totalPrice;
}
Aggregations