我试图理解测试案例
it('should not deadlock with concurrency duplicate entries and no outer transaction',
基本上是一个函数,FindorCreate称为50times,每个函数都会返回承诺。
FindOrcreate函数打开事务,是否找到选择,如果找不到行,则插入一行。我对其在
- 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
- 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?
发布评论
评论(1)
我想我现在明白为什么它不能挂了。
由于所有 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.