我正在学习 Spring JPA 和 Hibernate。所以我遇到了一个问题。
我有这个方法
@Transactional(isolation = Isolation.REPEATABLE_READ)
public void sendMoney(Long from, Long to, Double amount) {
WalletEntity fromWallet = walletServiceImpl.getWallet(from);
WalletEntity toWallet = walletServiceImpl.getWallet(to);
fromWallet.setAmount(fromWallet.getAmount() - amount);
toWallet.setAmount(toWallet.getAmount() + amount);
TransactionEntity transaction = new TransactionEntity();
transaction.setAmount(amount);
transaction.setToWallet(toWallet);
transaction.setFromWallet(fromWallet);
transactionRepository.saveAndFlush(transaction);
}
我想测试它并创建了这个:
@GetMapping("/send")
public void sendMoney() {
ExecutorService executorService = Executors.newFixedThreadPool(20);
for (int i = 0; i < 100; i++) {
executorService.execute(() -> {
accountServiceImpl.sendMoney(1L, 2L, 10D);
});
}
}
所以当我阅读 wallet 时,我得到了旧的价值,但我做了Isolation.REPEATABLE_READ. 数据库中的值当然是错误的。你能解释一下有什么问题吗?谢谢!
小怪兽爱吃肉
相关分类