Skip to content

Commit

Permalink
sync knex & DB transaction state on trx callback error throw
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jurko-gospodnetic committed Mar 8, 2016
1 parent 3d8131b commit 84f85be
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/transaction.js
Expand Up @@ -32,10 +32,15 @@ function Transaction(client, container, config, outerTx) {
return makeTransactor(this, connection, trxClient)
})
.then((transactor) => {
var result = container(transactor)

// If we've returned a "thenable" from the transaction container, assume
// the rollback and commit are chained to this object's success / failure.
// Directly thrown errors are treated as automatic rollbacks.
var result
try {
result = container(transactor)
} catch (err) {
result = Promise.reject(err)
}
if (result && result.then && typeof result.then === 'function') {
result.then((val) => {
transactor.commit(val)
Expand Down

0 comments on commit 84f85be

Please sign in to comment.