Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
MySQL Can not open connection after call connection.end() => Error: Pool is closed

config.js

const mysql = require('mysql2');

const config = {
    host: 'localhost',
    port: '3306',
    user: 'root',
    password: 'root',
    database: 'krometal',
    charset: "utf8mb4_bin",
    multipleStatements: true
};
const connection = mysql.createPool(config);

connection.getConnection(function (err, connection) {
    //connecting to database
    if (err) {
        logger.log('error', err);
        console.log("MYSQL CONNECT ERROR: " + err);
    } else {
        logger.log('info', "MYSQL CONNECTED SUCCESSFULLY.");
        console.log("MYSQL CONNECTED SUCCESSFULLY.");
    }
});

module.exports = {
    connection
}

login.js

const loginUser = async (req, callback) => {
  connection.query(sql, async function (err, rows) => {
      // logic
      callback(result, 401, connection);
  })
}

route.js

users.post('/users/login', async (req, res) => {
    await loginUser(req, async (response, code, connection) => {
        await connection.end();
        res.status(code).send(response);
    });
});

The problem is the first time I try login worked fine, but if I try the same login API again throw the error Error: Pool is closed.

Should I use the

connection.end()

method every time I open connection or mysql2 automatically end connection?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Don't run

await connection.end();

After running this function you need to create a new connection using

connection.getConnection();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!