[英]Flask-SQLAlchemy doesn't increment column consistently
I've build small web app and noticed when there are multiple actions at the same time where it requires database to get last record it doesn't get newest results我已经构建了小型网络应用程序,并注意到当同时有多个操作需要数据库获取最后一条记录时,它不会获得最新结果
For example:例如:
add_amount = 1
record = Table.query.filter_by(id=1).first()
record.amount += add_amount
db.session.commit()
if multiple people add record in same time it won't get newest data如果多人同时添加记录,则不会获得最新数据
What would be best solution to take care of it?照顾它的最佳解决方案是什么? create new db session?
创建新的数据库会话?
What you're running into is called a race condition
.您遇到的情况称为
race condition
。 It's a heavily studied subject within concurrency in general.总的来说,这是并发性中一个经过大量研究的主题。 It happens, for example, when the following order of operations occurs:
例如,当发生以下操作顺序时,就会发生这种情况:
Now you can see that the change made by A has been overwritten completely.现在您可以看到 A 所做的更改已被完全覆盖。 There is no record that it ever happened and it did not influence the current value of
amount
.没有任何记录表明它曾经发生过,也没有影响
amount
的当前值。
As per this answer , you can apply a lock that will allow only one process to have the row fetched at a time, and the lock is released on db.session.commit()
.根据这个答案,您可以应用一个锁,该锁一次只允许一个进程获取行,并在
db.session.commit()
上释放该锁。 The lock locks only that one row, so any other calls to any other records should be fine.锁只锁定那一行,所以对任何其他记录的任何其他调用都应该没问题。
record = Table.query.filter_by(id=1).with_for_update().first()
With the locking, the process would become:锁定后,过程将变为:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.