knex: Can't add new command when connection is in closed state

This happened to me today when i upgrade to 0.14.0. Not sure when the bug was introduced.

Also, i saw this along with the above message: This socket has been ended by the other party

Previously working version seems to be 0.13.0

node 8.9.1, knex 0.14.0, “mysql2”: “1.4.2”

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 44 (15 by maintainers)

Most upvoted comments

Hi, just today we’ve observed (by placing a few good ol’ console.logs) that the factory.validate function which knex.js provides to generic-pool is never called, which might explain how already-dead connections are still being dispensed from the resource pool. Supposedly, generic-pool will validate resources before dispensing, and if they seem broken, create and dispense a new one (in this case, connection) as a replacement. However, this only happens if the right options are passed to generic-pool: https://github.com/coopernurse/node-pool/blob/master/lib/Pool.js#L222

As seen in the code above, the pool will only actually run the validator function if the option testOnBorrow has been enabled. It is false by default, and knex.js does not seem to override it when creating the pool.

@rkaw92 I am testing this workaround since yesterday morning and it seems to work fine. Before that I had to restart API processes at least once a day, otherwise database connection will eventually close without reconnection.

@rkaw92 sounds good, now we just need a test case and make knex to enable that setting always. I will do that if I can get first one other bug fixed that I’m working on.

I found that this was being caused due to importing a large file. I fixed it by setting “max_allowed_packet=32505856”

Yeah, you can pass other pool options, too.

@leebenson One thing to try without code modification is to pass testOnBorrow to knex, like so:

const db = require('knex')({
  client: '...',
  connection: '...',
  pool: { testOnBorrow: true }
})

This should tell you if it’s caused by the connection recycling bug. Of course, it’s just a temporary workaround, but it would be helpful if we can see whether it’s this or another sneaky one.

If successful, at the time when the connection fails, you’re going to see this printed to console.log:

Knex:warning - Connection Error: Connection ended unexpectedly

And the next thing should be the pool allocating a fresh connection for you.