Skip to content

Instantly share code, notes, and snippets.

@saml
Created March 5, 2021 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saml/646ede1848603622bf44aa5b596f1467 to your computer and use it in GitHub Desktop.
Save saml/646ede1848603622bf44aa5b596f1467 to your computer and use it in GitHub Desktop.
/*
$ node sequelizeTransactionPoolSize.js
Started promises { count: 5 }
Executing (1cd366d4-a412-4a87-9185-0261f208169b): START TRANSACTION;
Executing (d9eadf8c-7407-4653-9a71-c3f83aee9ca2): START TRANSACTION;
Executing (d1c4e9e1-0fdf-4284-8cfc-9f0f60cc0ff1): START TRANSACTION;
Executing (c3ea6d09-b3e9-44dd-b277-1ac715f9c942): START TRANSACTION;
Executing (89824a8c-766d-490f-9b65-d0baab9b2030): START TRANSACTION;
it just hangs
*/
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('postgres://postgres@localhost:5432/postgres', {
seederStorage: 'sequelize',
dialect: 'postgres',
define: {
timestamps: false,
},
// logging: false,
pool: {
max: 5,
},
});
async function query() {
await sequelize.transaction(async (transaction) => {
const one = await sequelize.query('SELECT 1');
console.log('Queried one', one);
});
}
async function concurrentQuery({ count = 1 }) {
const promises = [];
for (let i = 0; i < count; i++) {
promises.push(query());
}
console.log('Started promises', { count: promises.length });
await Promise.all(promises);
}
function parseArgs(argv = process.argv) {
return argv.slice(argv.findIndex((x) => x === __filename) + 1);
}
const args = parseArgs();
const count = parseInt(args[0]) || 5;
concurrentQuery({ count })
.then(() => {
console.log('result', 'pool config', sequelize.config.pool);
})
.catch((err) => {
console.error(err);
})
.finally(() => {
sequelize.close()
.then(() => {
console.log('Closed sequelize');
})
.catch((err) => {
console.error(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment