From d1ba8e02878cc500edcd9fa06ce7ce2c0a56d632 Mon Sep 17 00:00:00 2001 From: zhangzhi Date: Thu, 24 Aug 2023 15:46:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96tac=20demo=E4=BB=A3=E7=A0=81,?= =?UTF-8?q?=20=E4=BD=BF=E7=94=A8enum=E4=BB=A3=E6=9B=BFint=E5=80=BC?= =?UTF-8?q?=E6=9D=A5=E5=8C=BA=E5=88=86=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../enums/ReadCommittedTransactionEnum.java | 22 ++++++++++++ .../dubbo/order/service/PaymentService.java | 5 +-- .../order/service/impl/OrderServiceImpl.java | 8 ++--- .../service/impl/PaymentServiceImpl.java | 34 +++++++++---------- .../enums/ReadCommittedTransactionEnum.java | 22 ++++++++++++ .../order/service/PaymentService.java | 5 +-- .../order/service/impl/OrderServiceImpl.java | 7 ++-- .../service/impl/PaymentServiceImpl.java | 7 ++-- 8 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/enums/ReadCommittedTransactionEnum.java create mode 100644 hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/enums/ReadCommittedTransactionEnum.java diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/enums/ReadCommittedTransactionEnum.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/enums/ReadCommittedTransactionEnum.java new file mode 100644 index 00000000..645551e5 --- /dev/null +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/enums/ReadCommittedTransactionEnum.java @@ -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; +} diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/PaymentService.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/PaymentService.java index a1c1a3f9..93fd797e 100644 --- a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/PaymentService.java +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/PaymentService.java @@ -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. @@ -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); } diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/OrderServiceImpl.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/OrderServiceImpl.java index c7720d78..c1ded682 100644 --- a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/OrderServiceImpl.java +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/OrderServiceImpl.java @@ -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; @@ -168,11 +168,11 @@ 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); @@ -180,7 +180,7 @@ public String orderPayWithReadCommitted(Integer count, BigDecimal amount) { e.printStackTrace(); } // 开启另一个事务 - paymentService.makePaymentWithReadCommitted(order, 2); + paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_JUST_SELECT); System.out.println("切面耗时:" + (System.currentTimeMillis() - start)); return "success"; diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/PaymentServiceImpl.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/PaymentServiceImpl.java index 584e6ca0..a89252f6 100644 --- a/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/PaymentServiceImpl.java +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-dubbo/hmily-demo-tac-dubbo-order/src/main/java/org/dromara/hmily/demo/tac/dubbo/order/service/impl/PaymentServiceImpl.java @@ -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. @@ -27,13 +27,13 @@ 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. @@ -41,7 +41,7 @@ */ @Service public class PaymentServiceImpl implements PaymentService { - + private static final Logger LOGGER = LoggerFactory.getLogger(PaymentServiceImpl.class); private final OrderMapper orderMapper; @@ -58,7 +58,7 @@ public PaymentServiceImpl(final OrderMapper orderMapper, this.accountService = accountService; this.inventoryService = inventoryService; } - + @Override @HmilyTAC public void makePayment(final Order order) { @@ -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); @@ -89,7 +89,7 @@ public void makePaymentWithNested(final Order order) { //扣除用户余额 accountService.paymentWithNested(buildAccountNestedDTO(order)); } - + @Override @HmilyTAC public void makePaymentWithNestedException(final Order order) { @@ -101,7 +101,7 @@ public void makePaymentWithNestedException(final Order order) { //扣除用户余额 accountService.paymentWithNestedException(buildAccountNestedDTO(order)); } - + @Override @HmilyTAC public String mockPaymentInventoryWithTryException(final Order order) { @@ -111,7 +111,7 @@ public String mockPaymentInventoryWithTryException(final Order order) { inventoryService.mockWithTryException(buildInventoryDTO(order)); return "success"; } - + @Override @HmilyTAC public String mockPaymentInventoryWithTryTimeout(final Order order) { @@ -121,7 +121,7 @@ public String mockPaymentInventoryWithTryTimeout(final Order order) { inventoryService.mockWithTryTimeout(buildInventoryDTO(order)); return "success"; } - + @Override @HmilyTAC public String mockPaymentAccountWithTryException(final Order order) { @@ -129,7 +129,7 @@ public String mockPaymentAccountWithTryException(final Order order) { accountService.mockTryPaymentException(buildAccountDTO(order)); return "success"; } - + @Override @HmilyTAC public String mockPaymentAccountWithTryTimeout(final Order order) { @@ -137,7 +137,7 @@ public String mockPaymentAccountWithTryTimeout(final Order order) { accountService.mockTryPaymentTimeout(buildAccountDTO(order)); return "success"; } - + @Override @HmilyTAC public String mockPaymentInventoryWithConfirmTimeout(final Order order) { @@ -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"; } @@ -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()); @@ -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()); diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/enums/ReadCommittedTransactionEnum.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/enums/ReadCommittedTransactionEnum.java new file mode 100644 index 00000000..17b94e92 --- /dev/null +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/enums/ReadCommittedTransactionEnum.java @@ -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; +} diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/PaymentService.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/PaymentService.java index 9de7054d..4a89d98e 100644 --- a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/PaymentService.java +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/PaymentService.java @@ -18,6 +18,7 @@ import org.dromara.hmily.demo.common.order.entity.Order; +import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum; /** * PaymentService. @@ -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); } diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/OrderServiceImpl.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/OrderServiceImpl.java index 325fa5f6..8fb9e44c 100644 --- a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/OrderServiceImpl.java +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/OrderServiceImpl.java @@ -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; @@ -136,11 +137,11 @@ 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); @@ -148,7 +149,7 @@ public String orderPayWithReadCommitted(Integer count, BigDecimal amount) { e.printStackTrace(); } // 开启另一个事务 - paymentService.makePaymentWithReadCommitted(order, 2); + paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.READ_COMMITTED_TRANSACTION_JUST_SELECT); System.out.println("切面耗时:" + (System.currentTimeMillis() - start)); return "success"; diff --git a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/PaymentServiceImpl.java b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/PaymentServiceImpl.java index c22aad3a..e44e0a5b 100644 --- a/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/PaymentServiceImpl.java +++ b/hmily-demo/hmily-demo-tac/hmily-demo-tac-springcloud/hmily-demo-tac-springcloud-order/src/main/java/org/dromara/hmily/demo/springcloud/order/service/impl/PaymentServiceImpl.java @@ -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; @@ -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; @@ -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"; } @@ -203,4 +203,5 @@ private AccountNestedDTO buildAccountNestedDTO(Order order) { nestedDTO.setCount(order.getCount()); return nestedDTO; } + }