problem description
update funds multiple times in a function, and transaction log needs to be recorded for each change.
you need to re-query the changed Amount for each fund change.
related codes
//
long amountWX = 100;
int up = UserAccountMapper.updateAccount(id, amountWX);
//
if (createAccountLog(id, rid, amountWX) == 0) {
throw new RuntimeException("");
}
//
long amountAli = 200;
int up = UserAccountMapper.updateAccount(id, amountAli);
//
if (createAccountLog(id, rid, amountAli) == 0) {
throw new RuntimeException("");
}
//
long amountJD = 300;
int up = UserAccountMapper.updateAccount(id, amountJD);
//
if (createAccountLog(id, rid, amountJD) == 0) {
throw new RuntimeException("");
}
protected int createAccountLog(long id, long rid, long amount) {
AccountLog accountLog = new AccountLog();
accountLog.setUserId(id);
accountLog.setType(rid);
accountLog.setChangeTime(System.currentTimeMillis());
//
accountLog.setCurrAmount(amount);
//
Account account = UserAccountMapper.getUserAccount(id);
accountLog.setTotalAmount(account.getAmount);
return accountLogMapper.save(accountLog);
}
the solution currently in mind is to record the amount of each update to memory and batch insert for the last time. This solves the operation of save logs for many times, but you still need to query the financial situation after each change. Is there any other solution?
second revision:
@Transactional(rollbackFor = Exception.class)
public SuccessOrderDTO doPay(GoodsBean goodsBean) throws Exception {
try {
SuccessOrderDTO successOrderDTO = new SuccessOrderDTO();
//
if (userAccountMapper.userAccountByExpend(/***/) == 0) {
throw new BuyException();
}
if (createAccountLog(/***/) == 0) {
throw new AuctionException("");
}
//
if (userAccountMapper.accountByIncome(/***/) == 0) {
throw new BuyException();
}
//
if (createAccountLog(/***/) == 0) {
throw new AuctionException("");
}
//
if (createAccountLog(/***/) == 0) {
throw new AuctionException("");
}
//
if (createAccountLog(/***/) {
throw new AuctionException("");
}
successOrderDTO.setTypeName(csAuctionType.getTypeName());
successOrderDTO.setGoodsPrice(goodsPrice.toString());
successOrderDTO.setVouchersNum(csAuctionType.getGiftVoucherNum());
successOrderDTO.setGoodsName(csAuctionOrder.getGoodsName());
successOrderDTO.setOrderNo(csAuctionOrder.getOrderNo());
successOrderDTO.setAuctionUserName(csAuctionOrder.getRefAuctionUserName());
successOrderDTO.setCreateOrderTime(csAuctionOrder.getCreateTime());
return successOrderDTO;
} catch (Exception e) {
log.error("=========Exception============ {} =============", e.getMessage());
throw e;
}
}