Commit bbf47a0668fb33203e79504278e0e46c2a4d00a4
Merge remote-tracking branch 'github/master-jdk17' into master-jdk17
Showing
1 changed file
with
15 additions
and
9 deletions
yudao-module-pay/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java
| @@ -153,7 +153,7 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -153,7 +153,7 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 153 | 153 | ||
| 154 | // 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯) | 154 | // 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯) |
| 155 | return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> { | 155 | return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> { |
| 156 | - // 2. 扣除余额 | 156 | + // 2.1 扣除余额(CAS 原子扣减,先于重读,确保重读拿到扣减后最新值) |
| 157 | int updateCounts; | 157 | int updateCounts; |
| 158 | switch (bizType) { | 158 | switch (bizType) { |
| 159 | case PAYMENT: { | 159 | case PAYMENT: { |
| @@ -173,12 +173,14 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -173,12 +173,14 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 173 | throw exception(WALLET_BALANCE_NOT_ENOUGH); | 173 | throw exception(WALLET_BALANCE_NOT_ENOUGH); |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | + // 2.2 CAS 后重读钱包(事务内读自身更新,拿到扣减后最新余额;锁外预读的 balance 在并发时已过期, | ||
| 177 | + // 且 @Transactional REPEATABLE READ 下 CAS 前重读仍读事务快照旧值,须 CAS 后重读) | ||
| 178 | + PayWalletDO currentWallet = getWallet(walletId); | ||
| 179 | + | ||
| 176 | // 3. 生成钱包流水 | 180 | // 3. 生成钱包流水 |
| 177 | - // 情况一:充值退款:balance 在冻结时已扣,updateWhenRechargeRefund 只扣 freeze_price,所以 afterBalance 不变。https://t.zsxq.com/OJk9m | ||
| 178 | - // 情况二:消费支付:updateWhenConsumption 从 balance 扣,所以 afterBalance = balance - price | ||
| 179 | - Integer afterBalance = bizType == PayWalletBizTypeEnum.RECHARGE_REFUND | ||
| 180 | - ? payWallet.getBalance() | ||
| 181 | - : payWallet.getBalance() - price; | 181 | + // 情况一:充值退款:balance 在冻结时已扣,updateWhenRechargeRefund 只扣 freeze_price,balance 不变,afterBalance=当前余额 |
| 182 | + // 情况二:消费支付:updateWhenConsumption 从 balance 扣 price,CAS 后重读的 balance 即扣减后值,afterBalance=当前余额 | ||
| 183 | + Integer afterBalance = currentWallet.getBalance(); | ||
| 182 | WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId()) | 184 | WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId()) |
| 183 | .setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId)) | 185 | .setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId)) |
| 184 | .setBizType(bizType.getType()).setTitle(bizType.getDescription()); | 186 | .setBizType(bizType.getType()).setTitle(bizType.getDescription()); |
| @@ -200,7 +202,7 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -200,7 +202,7 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 200 | 202 | ||
| 201 | // 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯) | 203 | // 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯) |
| 202 | return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> { | 204 | return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> { |
| 203 | - // 3. 更新钱包金额 | 205 | + // 2.1 更新钱包金额(CAS 原子加,先于重读,确保重读拿到加值后最新值) |
| 204 | switch (bizType) { | 206 | switch (bizType) { |
| 205 | case PAYMENT_REFUND: { // 退款更新 | 207 | case PAYMENT_REFUND: { // 退款更新 |
| 206 | walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price); | 208 | walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price); |
| @@ -219,9 +221,13 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -219,9 +221,13 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 219 | } | 221 | } |
| 220 | } | 222 | } |
| 221 | 223 | ||
| 222 | - // 4. 生成钱包流水 | 224 | + // 2.2 CAS 后重读钱包(事务内读自身更新,拿到加值后最新余额;锁外预读的 balance 在并发时已过期, |
| 225 | + // 且 @Transactional REPEATABLE READ 下 CAS 前重读仍读事务快照旧值,须 CAS 后重读) | ||
| 226 | + PayWalletDO currentWallet = getWallet(walletId); | ||
| 227 | + | ||
| 228 | + // 3. 生成钱包流水(afterBalance = CAS 后最新余额,无需再 +price) | ||
| 223 | WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO() | 229 | WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO() |
| 224 | - .setWalletId(payWallet.getId()).setPrice(price).setBalance(payWallet.getBalance() + price) | 230 | + .setWalletId(payWallet.getId()).setPrice(price).setBalance(currentWallet.getBalance()) |
| 225 | .setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription()); | 231 | .setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription()); |
| 226 | return walletTransactionService.createWalletTransaction(transactionCreateReqBO); | 232 | return walletTransactionService.createWalletTransaction(transactionCreateReqBO); |
| 227 | }); | 233 | }); |