Repository navigation
Description
What are you doing?
hire is my two tests. Both of them do same things in different syntax. First one is success. Second one is NOT. As you can see in output second test executes each operation in separate transaction.
//bootstrap.js
const namespace = require('cls-hooked').createNamespace('Sequelize')
Sequelize.useCLS(namespace)
//test.js
it('async/await syntax', (done) => {
models.sequelize.transaction(async (t1) => {
await model.sequelize.query("select 1", {type: model.Sequelize.QueryTypes.SELECT})
await model.sequelize.query("select 2", {type: model.Sequelize.QueryTypes.SELECT})
await model.sequelize.query("select 3", {type: model.Sequelize.QueryTypes.SELECT})
if (models.sequelize.Sequelize.cls.get('transaction') !== t1) {
done("transaction !== t1")
}
else{
done()
}
})
})
it('then.then.then syntax', (done) => {
models.sequelize.transaction((t1) => {
return model.sequelize.query("select 1", {type: model.Sequelize.QueryTypes.SELECT})
.then(() => {
return model.sequelize.query("select 2", {type: model.Sequelize.QueryTypes.SELECT})
})
.then(() => {
return model.sequelize.query("select 3", {type: model.Sequelize.QueryTypes.SELECT})
})
.then(() => {
if (models.sequelize.Sequelize.cls.get('transaction') !== t1) {
done("transaction !== t1")
}
else {
done()
}
})
})
})What do you expect to happen?
two tests should be done
What is actually happening?
Second test returns error
Output, either JSON or SQL
async/await syntax
Executing (f0b9c9cd-a86b-4076-ac97-247c4cc00f4c): START TRANSACTION;
Executing (f0b9c9cd-a86b-4076-ac97-247c4cc00f4c): select 1
Executing (f0b9c9cd-a86b-4076-ac97-247c4cc00f4c): select 2
Executing (f0b9c9cd-a86b-4076-ac97-247c4cc00f4c): select 3
Executing (f0b9c9cd-a86b-4076-ac97-247c4cc00f4c): COMMIT;then.then.then syntax
Executing (9a38ccda-29a9-4572-a006-3e13a2bf053d): START TRANSACTION;
Executing (9a38ccda-29a9-4572-a006-3e13a2bf053d): select 1
Executing (default): select 2
Executing (default): select 3
Executing (9a38ccda-29a9-4572-a006-3e13a2bf053d): COMMIT;
Dialect: postgres
__Sequelize version:4.37
__nodejs: 8.11.2 (without babel)
Activity
Alexsey commentedon May 4, 2018
If your first code is working then probably you are using cls-hooked and
sequelizeby default is using internallybluebird, but it is not compatible with async_hook thatcls-hookedis based on. You can try to switchsequelizeto native promises or try some other workarounds based on thissushantdhiman commentedon May 25, 2018
Fixed by #9473