Skip to content

hooks do not get CLS transaction #12973

Closed

Description

@heroic

Issue Description

If using CLS to set a transaction, then the transaction is not received in the hooks.

What are you doing?

We set a transaction on the HTTP request using CLS. We expect the transaction to be available inside the hooks to be able to use the afterCommit transaction hook. However, options does not contain the transaction.

Here is the link to the SSCCE for this issue: LINK-HERE

This is inside a Koa2 app.

app.use(async (_, next) => {
  await sequelize.transaction(() => next());
});

In the model hook

beforeSave: (instance, options) => {
  console.warn(options.transaction);
},

The output is undefined.

What do you expect to happen?

options.transaction should be the same as the transaction set in the CLS.

What is actually happening?

options.transaction is undefined

undefined

Additional context

It appears that this logic is missing inside save:

if (options.transaction === undefined && this.sequelize.constructor._cls) {

Adding this to save fixes this issue.

Environment

  • Sequelize version: 6.4.0
  • Node.js version: 12.20.0
  • Operating System: Mac OSX

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start, but I don't know if this was left out intentionally
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. heroic commented on Jan 24, 2021

    @heroic
    ContributorAuthor

    Also discussed here #8585

  2. heroic commented on Jan 29, 2021

    @heroic
    ContributorAuthor

    @papb could you take a look at this? I can raise a PR for this, is this is a miss and there is no real reason to have skipped passing the transaction to the hooks

  3. github-actions commented on Oct 27, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  4. heroic commented on Oct 28, 2021

    @heroic
    ContributorAuthor

    not stale

  5. heroic commented on Oct 28, 2021

    @heroic
    ContributorAuthor

    @papb Any updates here?

  6. github-actions commented on Nov 6, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  7. heroic commented on Nov 7, 2021

    @heroic
    ContributorAuthor

    not stale

  8. github-actions commented on Nov 23, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  9. heroic commented on Nov 23, 2021

    @heroic
    ContributorAuthor

    not stale

  10. added
    topic: cls
    type: bug
    on Nov 26, 2021
  11. WikiRik commented on Nov 26, 2021

    @WikiRik
    Member

    Sorry about the late response, it would be nice if you could make a PR for this @heroic

  12. heroic commented on Jan 9, 2022

    @heroic
    ContributorAuthor

    @WikiRik Added!

  13. github-actions commented on Aug 7, 2022

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 7.0.0-alpha.15 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    released on @v7topic: clstype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions