knex: Knex:Error Pool2 - error: too many connections for role

@myndzi

We’re having issues with our database lately. Our settings are:

{
    client: 'postgresql',
    connection: {
      database: 'honestpacket'
    },
    pool: {
      min: 2,
      max: 10
    },
    seeds: {
        directory: './seeds'
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: './migrations'
    }
  }

We are not calling knex.destroy() anywhere so we aren’t explicitly destroying any connections. Here is the full stacktrace from the error:

2015-10-15T13:53:04.287266+00:00 app[web.2]: Unhandled rejection Error: Pool is destroyed
2015-10-15T13:53:04.287266+00:00 app[web.2]:     at Pool.acquire (/app/node_modules/knex/node_modules/pool2/lib/pool.js:163:12)
2015-10-15T13:53:04.287266+00:00 app[web.2]:     at /app/node_modules/knex/lib/client.js:204:19
2015-10-15T13:53:04.287267+00:00 app[web.2]:     at tryCatcher (/app/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)
2015-10-15T13:53:04.287267+00:00 app[web.2]:     at Promise._resolveFromResolver (/app/node_modules/knex/node_modules/bluebird/js/main/promise.js:480:31)
2015-10-15T13:53:04.287267+00:00 app[web.2]:     at new Promise (/app/node_modules/knex/node_modules/bluebird/js/main/promise.js:70:37)
2015-10-15T13:53:04.287268+00:00 app[web.2]:     at Client.acquireConnection (/app/node_modules/knex/lib/client.js:200:12)
2015-10-15T13:53:04.287268+00:00 app[web.2]:     at /app/node_modules/knex/lib/runner.js:138:49
2015-10-15T13:53:04.287268+00:00 app[web.2]:     at tryCatcher (/app/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)
2015-10-15T13:53:04.287269+00:00 app[web.2]:     at Function.Promise.attempt.Promise.try (/app/node_modules/knex/node_modules/bluebird/js/main/method.js:31:24)
2015-10-15T13:53:04.287269+00:00 app[web.2]:     at Runner.ensureConnection (/app/node_modules/knex/lib/runner.js:137:26)
2015-10-15T13:53:04.287269+00:00 app[web.2]:     at Runner.run (/app/node_modules/knex/lib/runner.js:30:31)
2015-10-15T13:53:04.287270+00:00 app[web.2]:     at QueryBuilder.Target.then (/app/node_modules/knex/lib/interface.js:27:43)
2015-10-15T13:53:04.287270+00:00 app[web.2]:     at QueryBuilder.tryCatcher (/app/node_modules/bookshelf/node_modules/bluebird/js/main/util.js:26:23)
2015-10-15T13:53:04.287271+00:00 app[web.2]:     at doThenable (/app/node_modules/bookshelf/node_modules/bluebird/js/main/thenables.js:52:38)
2015-10-15T13:53:04.287271+00:00 app[web.2]:     at tryConvertToPromise (/app/node_modules/bookshelf/node_modules/bluebird/js/main/thenables.js:30:20)
2015-10-15T13:53:04.287271+00:00 app[web.2]:     at Promise._resolveCallback (/app/node_modules/bookshelf/node_modules/bluebird/js/main/promise.js:442:24)
2015-10-15T13:53:04.287272+00:00 app[web.2]:     at Promise._settlePromiseFromHandler (/app/node_modules/bookshelf/node_modules/bluebird/js/main/promise.js:515:17)
2015-10-15T13:53:04.287272+00:00 app[web.2]:     at Promise._settlePromiseAt (/app/node_modules/bookshelf/node_modules/bluebird/js/main/promise.js:581:18)
2015-10-15T13:53:04.287272+00:00 app[web.2]:     at Promise._settlePromises (/app/node_modules/bookshelf/node_modules/bluebird/js/main/promise.js:697:14)
2015-10-15T13:53:04.287273+00:00 app[web.2]:     at Async._drainQueue (/app/node_modules/bookshelf/node_modules/bluebird/js/main/async.js:123:16)
2015-10-15T13:53:04.287275+00:00 app[web.2]:     at Async._drainQueues (/app/node_modules/bookshelf/node_modules/bluebird/js/main/async.js:133:10)
2015-10-15T13:53:04.287275+00:00 app[web.2]:     at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/bookshelf/node_modules/bluebird/js/main/async.js:15:14)
2015-10-15T13:53:04.287276+00:00 app[web.2]:     at processImmediate [as _immediateCallback] (timers.js:368:17)

We’re at a loss. Any help on how to debug this would be appreciated. Its especially hard since the error is intermittent. It’ll be fine for a day or two with active development and then reappear and halt everything.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (4 by maintainers)

Most upvoted comments

@elhigu but the pool configuration is optional. Starters might not be aware of it. I think it’s good to have a “safe by default” design, that is, user can use a very small set of parameters to create Knex instance and then start using it without problems.

Anyway, for the doc, I think we can add a “notice” or “best practice” at the end of the Initializing the library section. But English is not my primary language and I’m not quite good at English writing. It might be a bit difficult for me to write formal docs. 😦

I encountered this error yesterday. In my case, it’s because I create Knex instance every time when I need to access the database. But each Knex instance maintains a connection pool internally, and by default, each connection pool has at least 2 connections alive. Then you know how many connection pools will be created and how many connections will be open if I create knex instance every time.

The fix is simple in my case: just make knex singleton, that is, share the knex instance in the application.

@tgriesser Am I missing something? If what I mentioned above is correct, I would suggest to emphasize this in the documentation, so users will know they need to share Knex instance in the app. Or, maybe a better approach is to share connection pool inside knex.