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

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

说明:

我在数据库中有一张表,比如 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

我使用 Sequelize ORM 在 node.js 中创建了 API,名为 UpdateAppNo.

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 包装使用 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:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   sequelize

发布评论

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

>www.elefans.com

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