说明:
我在 db 中有一个表说 Table1.只有一列 AppNo(numeric) 并且只有一行,当前值为 0
I have one table in db say Table1. having only one column AppNo(numeric) and only single row, and current value is 0
我在 node.js 中使用名为 UpdateAppNo 的 Sequelize ORM 创建了 API.
I have created API in node.js using Sequelize ORM named UpdateAppNo.
每当 UpdateAppNo api 调用 AppNo 的值应该增加 1.
Whenever UpdateAppNo api called value of AppNo should increment by 1.
我想要什么:
如果一次有 2 个或更多并发请求,则当前请求应等到前一个请求完成.
If 2 or more simultaneous request comes at a time, current request should wait until previous request to complete.
现在发生了什么:
如果前一个请求正在处理中,那么当前请求会抛出一个错误.
If previous request is in process, then current request throws an error.
推荐答案这不是很好的文档或容易找到,但你想要的是 Transaction 包装你的 find 查询,它使用 lock 选项将其转换为 SELECT FOR UPDATE 语句.
This is not very well documented or easy to find, but what you want is a Transaction that wraps your find query which uses a lock option to turn it into a SELECT FOR UPDATE statement.
带有 lock 选项的查找查询将锁定行(您不需要锁定整个表),并且由于您使用的是事务,因此操作将被提交或回滚取决于您的实例是否持有锁.
The find query with the lock option will lock the row (you don't need to lock the entire table) and because you are using a transaction, the action will either be committed or rolled back depending on whether or not your instance holds the lock.
您需要调整 这些示例供您使用.
You will want to adapt the code found in these examples for your use.
更多推荐
如何在sequelize中锁定表,等到另一个请求完成
发布评论