3
我在我的数据库中有'钱'字段的'银行'表,节点js。续订交易
用户可以定期提款,但只有在银行存款> 0时才可以提款。
首先,我应该得到银行的实体,然后检查是否(银行。货币> amountToWithdraw),然后撤回此金额。
想象一下当并发用户试图提取一些钱时的情况。 在那个时候我检查是否(bank.money> amountToWithdraw)其他用户可以执行提取操作,并且数据库中的真实银行金额会更少。
如何将交易应用于查找银行操作(如何锁定银行实体)?
models.sequelize.transaction(function (t) {
return models.Banks.findOne({where: {
money: {
$gt: 0
}
}).then(function(bank){
//in this moment other user finished the same operation
// how to lock access for editing bank object by other users after //findOne method?
bank.money -= amountToWithdraw;
return bank.save({transaction: t});
})
})
你无法理解我花了多少时间寻找一个好的解决方案。我认为文档不太清楚交易使用情况。你的问题拯救了我的生命! –
如何使用两行,例如在一行中我有数量10,在其他20,我想减少3,并给其他,所以新的数量将是7和23? – user1735921