What is causing "TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full"

I have a cron job that runs everyday, some days I get this error

{ TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?

I don’t know what is causing this error but I suspect that it may be caused by the fact that I use Database Transactions. The way I use Database Transactions, I start with:

const trx = await Database.beginTransaction()

Then I use this line after the last query.:

await trx.commit()

But I don’t include this line in my catch block:

await trx.rollback()

This is because I assume that if an error occurs while running one of the queries, an exception will be thrown and the trx.commit() will never be called.

Could it be because of this that the Timeout error is occurring? Because my cron job runs a loop doing that could possibly throw an error multiple times.

Check for connection limit for Knex and DB.
If I remember correctly Knex default limit was 20 while MySQL default limit was 200. You might hit one of those limits.

Other thing to check for is slow running queries so queries start to pile up and fill the limit. You can enable slow query log for DB to find them (how to enable it differs on DB)

In /config/database.js you can set debug to true or set DEBUG=knex:* env variable for debuggint

We also noticed same error in analytical server when heavy queries ran that clogged up whole DB for several minutes and next connections failed to start

1 Like

Thanks for the reply. I will try this out