You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"after" Hooks in combination with transactions - opinion wanted #8115
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.
Reacted by Weston Siegenthaler, Clark Pan, Chris Kee, linpekka, heleg, Aerex and wjq152
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):
consttransHooks=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 successfullyfunctionaddTransCommitHookHandling(transaction,hookfn){if(!isfunc(hookfn))return;if(!transaction)returnhookfn();if(!transaction[transHooks]){transaction[transHooks]=[];constorigfn=transaction.commit;transaction.commit=function(...args){constprom=origfn.call(this, ...args),run_hooks=v=>transaction[transHooks].forEach(fn=>fn())&&v;// the following is possibly an overly defensive check for a Promise ...returnisfunc(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:
functionmyAfterCreateHook(instance,options){// options.transaction is defined only if there is a transaction in playaddTransCommitHookHandling(options.transaction,()=>{// here we would implement our actual "after create" hook logic });}
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 🙂
Activity
iamjochem commentedon Aug 16, 2017
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):
the
addTransCommitHookHandlingfunction is then used inside a hook handler function like so:stale commentedon Sep 22, 2017
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 🙂
iamjochem commentedon Sep 29, 2017
I'm still interested in opinions on this issue
linpekka commentedon Nov 3, 2017
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.