0
votes

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?

1

1 Answers

0
votes

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.