I have a route that uses the below shown method, in a node app that uses express.
I create a transaction
but don't use it in the update
method. Sequelize
is configured to not use managed transactions and auto commit is set to false.
When this route is called multiple times/under load around 7 - 10 times per second (the number of calls differ), I end up with 5 or so dangling transactions even though commit is called for the transaction at the end of the method call. (Because of these dangling transactions, subsequent calls and my node app is not able to make anymore db calls)
But if I pass the transaction
in the params
object this behaviour doesnt occur. And I don't get dangling transactions.
What can be the reason this is happening?
updateItem = (obj) => {
this.logDebug(`Updating item - `, obj);
return new Promise(async (resolve, reject) => {
let transaction;
try {
transaction = await this.getTransaction();
} catch(error) { return reject(error);
const params = {
where: {
id: obj.id
},
returning: true,
plain: true
};
return models[modelName].
update(obj, params).then(result => {
if (!result) { return result; }
result = JSON.parse(JSON.stringify(result[1]));
return result;
}).
then(async (result) => {
await transaction.commit();
return resolve(result);
}).
catch(async error => {
this.logError(`Failed to update - `, error);
await transaction.rollback();
return reject(error);
});
});
};
Causes dangling transaction.
const params = {
where: {
id: obj.id
},
returning: true,
plain: true
};
No dangling transactions occur.
const params = {
where: {
id: obj.id
},
returning: true,
plain: true,
transaction
};
Using Nodejs 12.4
, Sequelize 5.21.9
, postgres 9.x
Just using the created transaction
in the params
object somehow doesn't cause dangling transaction.
While NOT using the transaction
in the params
causes this issue of dangling transactions.
Wanted to know the cause of this behaviour? Is it a bug with my code? Or bug with Sequelize
?