JavaEar 专注于收集分享传播有价值的技术资料

ER_CON_COUNT_ERROR:knex和bookshelf连接太多 (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?

1个回答

    最佳答案

  1. 英文原文

    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.


    中文翻译

    是。如果为每个请求创建新的knex实例,则无法控制到mysql数据库的并发连接总数。此外,您将无法重新使用来自knex连接池的已打开连接,因此在每个查询上打开与数据库的新TCP连接非常低效。此外,如果您在查询后没有销毁您的knex实例,连接将保持打开状态,直到某些空闲超时+ app将泄漏内存。

    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.

    是。如果为每个请求创建新的knex实例,则无法控制到mysql数据库的并发连接总数。此外,您将无法重新使用来自knex连接池的已打开连接,因此在每个查询上打开与数据库的新TCP连接非常低效。此外,如果您在查询后没有销毁您的knex实例,连接将保持打开状态,直到某些空闲超时+ app将泄漏内存。