Nodejs application using knex breaks after inserting few records into the Postgres database
Asked Answered
S

1

7

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('*')));
};
Snob answered 10/6, 2018 at 12:13 Comment(2)
Cannot reproduce, add complete code to test it out. Probably your observable code is not doing what you are thinking it does and is leaking connections.Pram
were you able to solve this?Petula
J
0

Try this, It solved my problem:

This works:

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

This does not work:

finally(knex.destroy());
Johannisberger answered 26/3, 2022 at 18:40 Comment(1)
that is normal js syntax error : you want to pass knex.destroy to the finally call, not the result of calling knex.destroy(). finally(knex.destroy); would work as expectedChristmas

© 2022 - 2024 — McMap. All rights reserved.