'Nodejs application using knex breaks after inserting few records into the Postgres database

My Nodejs application using knex breaks after inserting few records into the Postgres database with the following error:

Unhandled rejection Error

aborted at PendingOperation.abort (/mnt/c/Users/*********/node_modules/tarn/lib/PendingOperation.js:25:17)
at Promise.all.pendingAcquires.map.acquire (/mnt/c/Users/*********/node_modules/tarn/lib/Pool.js:190:23)
at Array.map (native)
at Promise.all.then.then (/mnt/c/Users/*********/node_modules/tarn/lib/Pool.js:189:34)

From previous event:

at Client_PG.acquireConnection (/mnt/c/Users/*********/node_modules/knex/lib/client.js:335:34)
at /mnt/c/Users/*********/node_modules/knex/lib/transaction.js:191:41

From previous event:

at Transaction.acquireConnection (/mnt/c/Users/*********/node_modules/knex/lib/transaction.js:190:34)
at new Transaction (/mnt/c/Users/*********/node_modules/knex/lib/transaction.js:68:53)
at Client_PG.transaction (/mnt/c/Users/*********/node_modules/knex/lib/client.js:159:12)
at Function.transaction (/mnt/c/Users/*********/node_modules/knex/lib/util/make-knex.js:75:21)

Here is the code where it breaks:

const writeOrdersToDB$ = (order: Order): Observable<Order> => {
  const orderToInsert = cleanOrder(order);
  return Observable.fromPromise(knex.transaction((trx: Transaction) => trx('order')
    .insert(orderToInsert)
    .returning('*')));
};


Solution 1:[1]

Try this, It solved my problem:

This works:

finally(x => knex.destroy());

This does not work:

finally(knex.destroy());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ahuemmer