Search code examples
mysqlnode.jsexpressknex.jsbookshelf.js

ER_CON_COUNT_ERROR: Too many connections knex and bookshelf


I've a simple rest api built with express, knex and bookshelf.

I'm doing some performance test with Jmeter and I've noticed that if I call the API that perform the following query there is no problem:

public static async fetchById(id: number): Promise<DatasetStats> {
      return DatasetStats.where<DatasetStats>({ id }).fetch();
 }

DatasetStats is a Bookshelf model

But If I set Jmeter to call the following I got a Error: ER_CON_COUNT_ERROR: Too many connections after a minute:

import * as knex from 'knex';

@injectable()
export class MyRepo {
  private knex: knex;

  constructor() {this.knex = knex(DatabaseConfig); }

  async fetchResourcesList(datasetName: string): Promise<any> {
      return this.knex.distinct('resource').from(datasetName);
  }
}

The problem could be that I create a knex object for each request?


Solution

  • Yes. If you create new knex instance for each request, you cannot control total number of concurrent connections to the mysql db. Also you won't be able to re-use already open connections from knex's connection pool, so it is highly inefficient to open new TCP connection to the database on every query. Also if you don't destroy your knex instances after the query, connections will be left open until some idle timeouts + app will leak memory.