用交易读写锁定模型FindOrcreate

发布于 2025-01-17 11:53:49 字数 751 浏览 0 评论 0 原文

我试图理解测试案例

  it('should not deadlock with concurrency duplicate entries and no outer transaction', 

基本上是一个函数,FindorCreate称为50times,每个函数都会返回承诺。 FindOrcreate函数打开事务,是否找到选择,如果找不到行,则插入一行。我对其在

  1. FindOrcreate具有以下SQLS运行;启动TXN,选择,插入,提交TXN。作为插入的一部分,必须在该行上采取独家锁定,该行仅在运行SQL“ Commit txn”后才发布。如果多次调用发现曲折的调用,则一个实例可以再次运行上述SQL,并且在运行查询“插入”时应该悬挂(甚至是循环块),因为尚未调用来自初级实例的Commit TXN的回调。

从痕迹中,我总是看到第二个实例插入在第一个提交之前永远不会发生。这只是运气吗?请帮助我理解。 Node-Postgres是否使用某种事件,而不仅仅是在独家锁上阻止?

I am trying to understand the test case https://github.com/sequelize/sequelize/blob/main/test/integration/model/create.test.js

  it('should not deadlock with concurrency duplicate entries and no outer transaction', 

Basically a function , findOrCreate is called 50times , each returns a promise.
findOrCreate function opens a transaction, does a select call and a row is inserted if row not found. I had the following query on how its implemented in https://github.com/brianc/node-postgres

  1. findOrCreate has following sqls to run; start txn, select, insert, commit txn. as part of insert an exclusive lock must be taken on that row which is released only after running sql "commit txn". If multiple calls to findOrCreate happen, one of the instance can again run the above sqls and when running query "insert" it should hang(even loop block) because the callback for commit txn from first instance has not yet been called.

From traces, I always see somehow the second instance insert never happens before the first commit. Is this just by luck? Please help me understand. Does node-postgres use some kind of events instead of just blocking on exclusive lock?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-01-24 11:53:49

我想我现在明白为什么它不能挂了。

由于所有 sql 都以非阻塞 I/O 方式执行,因此永远不会导致hang。
导致服务器上行锁的异步请求事务最终会以非阻塞方式发送提交 SQL 并获取数据就绪事件,从而释放服务器上的行锁。

I think I understand it now why it cant hang.

Since all the sqls are executed in non blocking I/O fashion, it would never result in hang .
async request transaction causing the row lock on server would eventually send commit sql in non blocking fashion and gets data ready event , resulting in releasing the row locks on server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文