如何在sequelize中锁定表,等到另一个请求完成

编程入门 行业动态 更新时间:2024-10-15 04:22:04
本文介绍了如何在sequelize中锁定表,等到另一个请求完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

说明:

我在 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中锁定表,等到另一个请求完成

本文发布于:2023-11-25 23:33:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!