Skip to content

Managed transaction not works with then.then.then syntax #9383

Closed

Description

@alexey2baranov

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

  1. Alexsey commented on May 4, 2018

    @Alexsey
    Contributor

    If your first code is working then probably you are using cls-hooked and sequelize by default is using internally bluebird, but it is not compatible with async_hook that cls-hooked is based on. You can try to switch sequelize to native promises or try some other workarounds based on this

  2. sushantdhiman commented on May 25, 2018

    @sushantdhiman
    Contributor

    Fixed by #9473

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions