Skip to content

Commit

Permalink
优化tac demo代码, 使用enum代替int值来区分代码逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zkyoma committed Aug 24, 2023
1 parent f86198d commit d1ba8e0
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.dromara.hmily.demo.tac.dubbo.order.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* The enum transaction enum.
*
* @author zhangzhi
*/
@RequiredArgsConstructor
@Getter
public enum ReadCommittedTransactionEnum {

READ_COMMITTED_TRANSACTION_ALL(1, "读已提交隔离级别的事务, 包括更新、查询操作"),

READ_COMMITTED_TRANSACTION_JUST_SELECT(2, "读已提交隔离级别的事务, 只有查询操作");

private final int code;

private final String desc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dromara.hmily.demo.tac.dubbo.order.service;

import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.tac.dubbo.order.enums.ReadCommittedTransactionEnum;

/**
* The interface Payment service.
Expand Down Expand Up @@ -97,7 +98,7 @@ public interface PaymentService {
* 订单支付.
*
* @param order 订单实体
* @param type 类型
* @param readCommittedTransactionEnum 事务类型
*/
String makePaymentWithReadCommitted(Order order, int type);
String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum readCommittedTransactionEnum);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package org.dromara.hmily.demo.tac.dubbo.order.service.impl;

import org.dromara.hmily.common.utils.IdWorkerUtils;
import org.dromara.hmily.demo.common.account.api.AccountService;
import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.common.order.enums.OrderStatusEnum;
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.tac.dubbo.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.tac.dubbo.order.service.OrderService;
import org.dromara.hmily.demo.tac.dubbo.order.service.PaymentService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -168,19 +168,19 @@ public String orderPayWithReadCommitted(Integer count, BigDecimal amount) {
// 开启一个事务
new Thread(() -> {
try {
paymentService.makePaymentWithReadCommitted(order, 1);
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_ALL);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}, "globl trans2").start();
}, "global trans2").start();
try {
// 确保第一个事务先执行
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 开启另一个事务
paymentService.makePaymentWithReadCommitted(order, 2);
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_JUST_SELECT);

System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
return "success";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -27,21 +27,21 @@
import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.common.order.enums.OrderStatusEnum;
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.tac.dubbo.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.tac.dubbo.order.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* PaymentServiceImpl.
* @author xiaoyu
*/
@Service
public class PaymentServiceImpl implements PaymentService {

private static final Logger LOGGER = LoggerFactory.getLogger(PaymentServiceImpl.class);

private final OrderMapper orderMapper;
Expand All @@ -58,7 +58,7 @@ public PaymentServiceImpl(final OrderMapper orderMapper,
this.accountService = accountService;
this.inventoryService = inventoryService;
}

@Override
@HmilyTAC
public void makePayment(final Order order) {
Expand All @@ -68,7 +68,7 @@ public void makePayment(final Order order) {
//进入扣减库存操作
inventoryService.decrease(buildInventoryDTO(order));
}

@Override
public void testMakePayment(final Order order) {
updateOrderStatus(order, OrderStatusEnum.PAYING);
Expand All @@ -89,7 +89,7 @@ public void makePaymentWithNested(final Order order) {
//扣除用户余额
accountService.paymentWithNested(buildAccountNestedDTO(order));
}

@Override
@HmilyTAC
public void makePaymentWithNestedException(final Order order) {
Expand All @@ -101,7 +101,7 @@ public void makePaymentWithNestedException(final Order order) {
//扣除用户余额
accountService.paymentWithNestedException(buildAccountNestedDTO(order));
}

@Override
@HmilyTAC
public String mockPaymentInventoryWithTryException(final Order order) {
Expand All @@ -111,7 +111,7 @@ public String mockPaymentInventoryWithTryException(final Order order) {
inventoryService.mockWithTryException(buildInventoryDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentInventoryWithTryTimeout(final Order order) {
Expand All @@ -121,23 +121,23 @@ public String mockPaymentInventoryWithTryTimeout(final Order order) {
inventoryService.mockWithTryTimeout(buildInventoryDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentAccountWithTryException(final Order order) {
updateOrderStatus(order, OrderStatusEnum.PAYING);
accountService.mockTryPaymentException(buildAccountDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentAccountWithTryTimeout(final Order order) {
updateOrderStatus(order, OrderStatusEnum.PAYING);
accountService.mockTryPaymentTimeout(buildAccountDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentInventoryWithConfirmTimeout(final Order order) {
Expand All @@ -153,9 +153,9 @@ public String mockPaymentInventoryWithConfirmTimeout(final Order order) {

@Override
@HmilyTAC
public String makePaymentWithReadCommitted(Order order, int type) {
public String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum transactionEnum) {
//第二个事务查询相同账户信息, 获取不到全局锁, 会进行回滚
if (type == 2) {
if (ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_JUST_SELECT.equals(transactionEnum)) {
accountService.findByUserId(order.getUserId());
return "success";
}
Expand All @@ -179,14 +179,14 @@ private void updateOrderStatus(final Order order, final OrderStatusEnum orderSta
order.setStatus(orderStatus.getCode());
orderMapper.update(order);
}

private AccountDTO buildAccountDTO(final Order order) {
AccountDTO accountDTO = new AccountDTO();
accountDTO.setAmount(order.getTotalAmount());
accountDTO.setUserId(order.getUserId());
return accountDTO;
}

private AccountNestedDTO buildAccountNestedDTO(final Order order) {
AccountNestedDTO nestedDTO = new AccountNestedDTO();
nestedDTO.setAmount(order.getTotalAmount());
Expand All @@ -195,7 +195,7 @@ private AccountNestedDTO buildAccountNestedDTO(final Order order) {
nestedDTO.setCount(order.getCount());
return nestedDTO;
}

private InventoryDTO buildInventoryDTO(final Order order) {
InventoryDTO inventoryDTO = new InventoryDTO();
inventoryDTO.setCount(order.getCount());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.dromara.hmily.demo.springcloud.order.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* The enum transaction enum.
*
* @author zhangzhi
*/
@RequiredArgsConstructor
@Getter
public enum ReadCommittedTransactionEnum {

READ_COMMITTED_TRANSACTION_ALL(1, "读已提交隔离级别的事务, 包括更新、查询操作"),

READ_COMMITTED_TRANSACTION_JUST_SELECT(2, "读已提交隔离级别的事务, 只有查询操作");

private final int code;

private final String desc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum;

/**
* PaymentService.
Expand Down Expand Up @@ -100,7 +101,7 @@ public interface PaymentService {
* 订单支付.
*
* @param order 订单实体
* @param type 类型
* @param readCommittedTransactionEnum 读已提交事务类型
*/
String makePaymentWithReadCommitted(Order order, int type);
String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum readCommittedTransactionEnum);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.common.order.enums.OrderStatusEnum;
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.springcloud.order.service.OrderService;
import org.dromara.hmily.demo.springcloud.order.service.PaymentService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -136,19 +137,19 @@ public String orderPayWithReadCommitted(Integer count, BigDecimal amount) {
// 开启一个事务
new Thread(() -> {
try {
paymentService.makePaymentWithReadCommitted(order, 1);
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_ALL);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}, "globl trans2").start();
}, "global trans2").start();
try {
// 确保第一个事务先执行
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 开启另一个事务
paymentService.makePaymentWithReadCommitted(order, 2);
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_JUST_SELECT);

System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
return "success";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.dromara.hmily.demo.springcloud.order.service.impl;

import org.dromara.hmily.annotation.HmilyTAC;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.common.account.dto.AccountDTO;
import org.dromara.hmily.demo.common.account.dto.AccountNestedDTO;
Expand All @@ -27,6 +26,7 @@
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.springcloud.order.client.AccountClient;
import org.dromara.hmily.demo.springcloud.order.client.InventoryClient;
import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.springcloud.order.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -154,9 +154,9 @@ public String mockPaymentInventoryWithConfirmTimeout(final Order order) {

@Override
@HmilyTAC
public String makePaymentWithReadCommitted(Order order, int type) {
public String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum transactionEnum) {
//第二个事务查询相同账户信息, 获取不到全局锁, 会进行回滚
if (type == 2) {
if (ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_JUST_SELECT.equals(transactionEnum)) {
accountClient.findByUserId(order.getUserId());
return "success";
}
Expand Down Expand Up @@ -203,4 +203,5 @@ private AccountNestedDTO buildAccountNestedDTO(Order order) {
nestedDTO.setCount(order.getCount());
return nestedDTO;
}

}

0 comments on commit d1ba8e0

Please sign in to comment.