javascript – Handling Sequelize Concurrent Requests in Node.js
I am currently having problems with handling concurrent requests using sequelize (sqlserver)
Let’s say I have an endpoint that runs the ff code:
async function increment() {
let data;
const t = await sequelize.transaction();
try {
const ctrData = await Counter.findOne(
{ where: { id: 1 } },
{ transaction: t }
);
if (ctrData) {
ctrData.currentValue++;
data = await ctrData.save({transaction: t});
}
t.commit();
return data;
} catch (error) {
t.rollback();
}
}
The endpoint will have a probability that it will be hit multiple times at the same time, if that happens then the currentValue will have the same increment value on those concurrent requests. Any idea on how I can make sure that each concurrent request has the correct values?
Read more here: Source link
