Skip to content

"after" Hooks in combination with transactions - opinion wanted #8115

Closed

Description

@iamjochem

hi guys,

I noticed that "after" hooks related to persistence (e.g. "afterUpdate" but not "afterBuild") do not honour transactions.

By which I mean the hooks fire before the transaction is commited and therefore also even when a transaction has been rolled back.

My feeling is that this is not correct because it leads to situations where "after" hooks are triggered for create, update & destroy actions where the actual DB mutation has never taken place (because it was rolled back).

This issue has been raised before (see here), but that is a discussion which has been running for over 2 years without any real resolution ... not suprising given that it's a big hairy thing to attempt to "fix", for most people an edge case that is not relevant and likely has a boat-load of potential BC implications.

My question is whether you agree that the following hooks should be transaction-aware and only fire if a transaction is committed:

  • afterCreate
  • afterUpdate
  • afterDestroy
  • afterBulkCreate
  • afterBulkUpdate
  • afterBulkDestroy

Assuming that you do agree with this, do you have any opinions/insights about how to best implement this?

P.S. Also I wonder whether it should be mentioned (the fact that the "after" hooks are not transaction aware) in the documentation.

Activity

  1. iamjochem commented on Aug 16, 2017

    @iamjochem
    ContributorAuthor

    To follow up on my question I offer the following snippet which I used in my own code to force "after" hook handlers to only fire if a transaction is committed - it works for my use case, maybe it's helpful for someone else and/or could form the basis of a "proper" fix (assuming you agree the issue is something that should be fixed):

    const transHooks    = Symbol('sequelize_transaction_hooks_property');   // used as an 'invisible' property on transaction objects, used to stored "after*" hook functions that should only run if the transaction actually commits successfully
    
    function addTransCommitHookHandling(transaction, hookfn)
    {
        if (!isfunc(hookfn))    return;
        if (!transaction)       return hookfn();
    
        if (!transaction[transHooks]) {
            transaction[transHooks] = [];
    
            const origfn = transaction.commit;
    
            transaction.commit = function(...args) {
                const prom      = origfn.call(this, ...args),
                      run_hooks = v => transaction[transHooks].forEach(fn => fn()) && v
                ;
    
                // the following is possibly an overly defensive check for a Promise ...
                return isfunc(prom && prom.then) ? prom.then(run_hooks) : run_hooks(prom);
            };
        }
    
        transaction[transHooks].push(hookfn);
    }

    the addTransCommitHookHandling function is then used inside a hook handler function like so:

    function myAfterCreateHook(instance, options)
    {
         // options.transaction is defined only if there is a transaction in play
         addTransCommitHookHandling(options.transaction, () => {
               // here we would implement our actual "after create" hook logic 
         });
    }
  2. stale commented on Sep 22, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  3. iamjochem commented on Sep 29, 2017

    @iamjochem
    ContributorAuthor

    I'm still interested in opinions on this issue

  4. linpekka commented on Nov 3, 2017

    @linpekka

    I am also looking for a solution for this issue, so I will give yours a try. Stale bot seems a bit aggressive these days.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions