L1uxiaolin commented on issue #615: URL: https://github.com/apache/incubator-seata-samples/issues/615#issuecomment-2027957300
> ``` > public boolean prepareMinus(BusinessActionContext businessActionContext, final String accountNo, > final double amount) { > //分布式事务ID > final String xid = businessActionContext.getXid(); > > return fromDsTransactionTemplate.execute(new TransactionCallback<Boolean>() { > > @Override > public Boolean doInTransaction(TransactionStatus status) { > try { > //校验账户余额 > Account account = fromAccountDAO.getAccountForUpdate(accountNo); > if (account == null) { > throw new RuntimeException("账户不存在"); > } > if (account.getAmount() - amount < 0) { > throw new RuntimeException("余额不足"); > } > //冻结转账金额 > double freezedAmount = account.getFreezedAmount() + amount; > account.setFreezedAmount(freezedAmount); > fromAccountDAO.updateFreezedAmount(account); > System.out.println(String > .format("prepareMinus account[%s] amount[%f], dtx transaction id: %s.", accountNo, amount, > xid)); > return true; > } catch (Throwable t) { > t.printStackTrace(); > status.setRollbackOnly(); > return false; > } > } > }); > } > ``` > > 这段代码中对FreezedAmount的操作有何意义? 不冻结的话,可能存在超出余额的转账事务存在。比如余额是100,A事务转50,B事务转100,这个时候B事务只会看到余额有100,不清楚其他事务是否也在转账中。加上第二阶段又必须成功,会导致两笔都成功。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@seata.apache.org For additional commands, e-mail: dev-h...@seata.apache.org