question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Too many connections with connection pool

See original GitHub issue

I’m getting too many connections error when I make exactly 30 insert queries on my node app.

I have a connect.js file like this:

async function connection() {
  try {
    const mysql = require('mysql2');

    const pool = mysql.createPool({
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DATABASE,
      connectionLimit: 10,
      waitForConnections: true,
      queueLimit: 0
    });

    const promisePool = pool.promise();

    return promisePool;
  } catch (error) {
    return console.log(`Could not connect - ${error}`);
  }
}

module.exports = {
  connection
};

and another file that I import the connect.jslike this:

let connection = require('connect');

let db = await connection();

then sql queries with db.execute(); follows.

Please help. Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
sidorarescommented, Aug 23, 2018

you are creating new pool each time instead of borrowing connection from the same pool. Change to something like this:

function createPool() {
  try {
    const mysql = require('mysql2');

    const pool = mysql.createPool({
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DATABASE,
      connectionLimit: 10,
      waitForConnections: true,
      queueLimit: 0
    });

    const promisePool = pool.promise();

    return promisePool;
  } catch (error) {
    return console.log(`Could not connect - ${error}`);
  }
}

const pool = createPool();

module.exports = {
  connection: async () => pool.getConnection()
  execute: (...params) => pool.execute(...params)
};
const db = require('./db');
const conn = await db.connection();
const results = conn.execute('select 1+1');
conn.release();

// or
const results = db.execute('select 1 + 1');
6reactions
PolymathWhizcommented, Nov 13, 2018
function connection() {
  try {
    const mysql = require('mysql2');

    const pool = mysql.createPool({
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DATABASE,
      timezone: 'utc',
      connectionLimit: 10,
      waitForConnections: true,
      queueLimit: 0
    });

    const promisePool = pool.promise();

    return promisePool;
  } catch (error) {
    return console.log(`Could not connect - ${error}`);
  }
}

const pool = connection();

module.exports = {
  connection: async () => pool.getConnection(),
  execute: (...params) => pool.execute(...params)
};

In another file

const db = require('path to file');

Then to run a query

const [query] = await db.execute(/*query here*/);

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL Error: Too many connections - Percona
Most of the time, an overly high number of connections is the result of either bugs in applications not closing connections properly or...
Read more >
Resolve "Too Many Connections" error when ... - AWS
Review existing connections and, if possible, terminate them to release connection pressure. For example, start by terminating connections in ...
Read more >
MySQL Error “Too many connections” and how to resolve it
When a client tries to log into MySQL it may sometimes be rejected and receive an error message saying that there are “too...
Read more >
MySQL 8.0 Reference Manual :: B.3.2.5 Too many connections
If clients encounter Too many connections errors when attempting to connect to the mysqld server, all available connections are in use by other...
Read more >
Solve MySQL/MariaDB "Too many connections" error - IONOS
The MySQL “Too many connections” error occurs when more queries are sent to a MySQL database than can be processed. The error can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found