Fix transaction callback throw handling #1257
Originally if a transaction callback threw an error,
Effect would be random database deadlocks and/or operations failures later on due to application code running under a transaction when it does not expect to be, e.g. it can cause that transaction to start holding database locks and thus block other regular transactions, and all of it at random depending on the order in which connections get grabbed from the
This changes the behaviour by treating such thrown exceptions the same as if a rejected promise had been returned from them and rolls back the underlying database transaction.
The pull request also does some minor code cleanup to keep the code directly touched by it in sync with the rest of the code.
Test suite updates:
Previously throwing an error directly from a transaction callback resulted in knex reporting that transaction's promise as rejected, and releasing used connection (back to the connection pool), but not telling the database to roll back that connection's transaction.
Could someone run the test suite on
One test currently reported as failing is completely random (happened only once and that after a documentation update commit
Same effect noticed on pull request #1258.
@wubzz - whom to ask to review/approve/merge this? If it helps, we can do a Google hangout session or something similar and I can help by explaining things directly.
Not being altruistic here, I'd just like to get this in so I can stop maintaining my own project forks for some projects that need these fixes.
We already do that partially by rejecting the transaction promise (the one returned by the
That is bad because we then put the database connection back into the underlying database connection pool, and anyone using it later on does their work in that transaction without knowing that the transaction even exists. Meaning - unexpected locks and/or errors...
So in fact, before this fix, knex is lying to the caller (and in fact, internally to itself) that the transaction has been rolled back.