Commit 6f14c195e877f63ac7bdf11eee8f658b5fc330e8
1 parent
bbf47a06
refactor(pay): 精简钱包余额流水修复代码
Showing
1 changed file
with
7 additions
and
11 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.1 扣除余额(CAS 原子扣减,先于重读,确保重读拿到扣减后最新值) | 156 | + // 2.1 扣除余额 |
| 157 | int updateCounts; | 157 | int updateCounts; |
| 158 | switch (bizType) { | 158 | switch (bizType) { |
| 159 | case PAYMENT: { | 159 | case PAYMENT: { |
| @@ -173,16 +173,13 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -173,16 +173,13 @@ 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 后重读) | 176 | + // 2.2 更新后重新查询钱包,避免使用锁外读取的旧余额生成流水 |
| 178 | PayWalletDO currentWallet = getWallet(walletId); | 177 | PayWalletDO currentWallet = getWallet(walletId); |
| 179 | 178 | ||
| 180 | // 3. 生成钱包流水 | 179 | // 3. 生成钱包流水 |
| 181 | - // 情况一:充值退款:balance 在冻结时已扣,updateWhenRechargeRefund 只扣 freeze_price,balance 不变,afterBalance=当前余额 | ||
| 182 | - // 情况二:消费支付:updateWhenConsumption 从 balance 扣 price,CAS 后重读的 balance 即扣减后值,afterBalance=当前余额 | ||
| 183 | - Integer afterBalance = currentWallet.getBalance(); | 180 | + // 充值退款时,余额已在冻结时扣除,本次只扣冻结金额。https://t.zsxq.com/OJk9m |
| 184 | WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId()) | 181 | WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId()) |
| 185 | - .setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId)) | 182 | + .setPrice(-price).setBalance(currentWallet.getBalance()).setBizId(String.valueOf(bizId)) |
| 186 | .setBizType(bizType.getType()).setTitle(bizType.getDescription()); | 183 | .setBizType(bizType.getType()).setTitle(bizType.getDescription()); |
| 187 | return walletTransactionService.createWalletTransaction(bo); | 184 | return walletTransactionService.createWalletTransaction(bo); |
| 188 | }); | 185 | }); |
| @@ -202,7 +199,7 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -202,7 +199,7 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 202 | 199 | ||
| 203 | // 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯) | 200 | // 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯) |
| 204 | return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> { | 201 | return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> { |
| 205 | - // 2.1 更新钱包金额(CAS 原子加,先于重读,确保重读拿到加值后最新值) | 202 | + // 2.1 更新钱包金额 |
| 206 | switch (bizType) { | 203 | switch (bizType) { |
| 207 | case PAYMENT_REFUND: { // 退款更新 | 204 | case PAYMENT_REFUND: { // 退款更新 |
| 208 | walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price); | 205 | walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price); |
| @@ -221,11 +218,10 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -221,11 +218,10 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 221 | } | 218 | } |
| 222 | } | 219 | } |
| 223 | 220 | ||
| 224 | - // 2.2 CAS 后重读钱包(事务内读自身更新,拿到加值后最新余额;锁外预读的 balance 在并发时已过期, | ||
| 225 | - // 且 @Transactional REPEATABLE READ 下 CAS 前重读仍读事务快照旧值,须 CAS 后重读) | 221 | + // 2.2 更新后重新查询钱包,避免使用锁外读取的旧余额生成流水 |
| 226 | PayWalletDO currentWallet = getWallet(walletId); | 222 | PayWalletDO currentWallet = getWallet(walletId); |
| 227 | 223 | ||
| 228 | - // 3. 生成钱包流水(afterBalance = CAS 后最新余额,无需再 +price) | 224 | + // 3. 生成钱包流水 |
| 229 | WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO() | 225 | WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO() |
| 230 | .setWalletId(payWallet.getId()).setPrice(price).setBalance(currentWallet.getBalance()) | 226 | .setWalletId(payWallet.getId()).setPrice(price).setBalance(currentWallet.getBalance()) |
| 231 | .setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription()); | 227 | .setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription()); |