website icon indicating copy to clipboard operation
website copied to clipboard

Clarify whether it is OK to fire concurrent queries using the same transaction

Open ksze opened this issue 4 years ago • 3 comments

Issue Creation Checklist

[x] I have read the contribution guidelines

Issue Description

What was unclear/insufficient/not covered in the documentation

It is popular to write asynchronous code in JavaScript. And because sequelize.query is async and returns a Promise, it can be very tempting to write code like this:

await sequelize.transaction(async (transaction) => {
  await Promise.all([
    sequelize.query(query1, {transaction}),
    sequelize.query(query2, {transaction}),
    /* and more queries ... */
    sequelize.query(queryN, {transaction}),
  ]);
});

However, it is not entirely clear from the documentation whether this is supposed to work. I mean, this might accidentally work if the queries are all very short and quick. But what happens if some queries take longer than others? What if the queries are indirectly/implicitly related (e.g. query2 depending on some changes effected in the database by query1). Does sequelize automatically order the queries (in the order that the sequelize.query() calls were made), even though they look to be fired concurrently? Would the correct functioning depend on how async is implemented in the JavaScript runtime; would it work correctly in node.js but be dangerous in a hypothetical alternative JavaScript runtime that is still conformant to ECMA-262?

If possible: Provide some suggestion on how we can enhance the docs

Explicitly explain whether concurrent queries can work correctly (i.e. the way the programmer might expect), maybe under specific conditions - e.g. no indirect/implicit dependency among the queries.

ksze avatar Jun 29 '21 06:06 ksze

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

github-actions[bot] avatar Oct 27 '21 18:10 github-actions[bot]

bump

ksze avatar Oct 28 '21 04:10 ksze

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

github-actions[bot] avatar Nov 05 '21 00:11 github-actions[bot]