I'm testing MySQL locally with Node, and every time I run the file, the program doesn't end unless I manually terminate it (with ctrl+c). I searched online, and I especially looked at this question (not a duplicate—it doesn't ask about my error), which said that the program will end if I call pool.end()
. However, when I call the function, it gives me an error: Pool is closed
, which leads me to believe that the pool is already closed. But then why doesn't my program end? My code looks something like this:
// database.js
const mysql = require("mysql2");
const {HOST, USER, DATABASE, PASSWORD} = process.env;
const pool = mysql.createPool({
host: HOST,
user: USER,
database: DATABASE,
password: PASSWORD,
waitForConnections: true,
connectionLimit: 50,
queueLimit: 0
});
exports.set = data => {
return new Promise((resolve, reject) => {
pool.execute("INSERT INTO `mytable` (name, email) VALUES (?, ?);", [data.name, data.email], function(err) {
if(err) reject(err);
else resolve();
});
});
};
exports.get = data => {
return new Promise((resolve, reject) => {
pool.query("SELECT * FROM `mytable` WHERE `name`=? AND `email`=?;", [data.name, data.email], function(err, result) {
if(err) reject(err);
else resolve(result);
});
});
};
exports.getAll = () => {
return new Promise((resolve, reject) => {
pool.query("SELECT * FROM `mytable`;", function(err, result) {
if(err) reject(err);
else resolve(result);
});
});
};
exports.end = () => {
pool.end();
};
And then in my main file:
const db = require("./database");
(async () => {
const data = { name: "John Doe", email: "johndoe@example.com" };
await db.set(data);
const result = await db.get(data);
console.log(result);
db.end(); // I added this line as mentioned in https://stackoverflow.com/questions/61976788/nodejs-program-not-exiting-after-completion
})();
It gives the error Pool is closed
with a stack trace. If I don't include the db.end()
line the program just won't end (at the moment db.end()
doesn't solve the problem either). Does anyone know the solution?
Update
As suggested in the comments, I installed a library called why-is-node-still-running
to assess the situation, but the logs didn't make much sense. Here are the logs for reference:
There are 6 handle(s) keeping the process running:
TCPWRAP
C:\[REDACTED]\node_modules\mysql
2\lib\connection.js:45 - this.stream = Net.connect(
C:\[REDACTED]\node_modules\mysql
2\lib\pool_connection.js:7 - super(options);
C:\[REDACTED]\node_modules\mysql
2\lib\pool.js:51 - connection = new PoolConnection(this, {
C:\[REDACTED]\node_modules\mysql
2\lib\pool.js:139 - this.getConnection((err, conn) => {
C:\[REDACTED]\database.js:39
- pool.query("SELECT * FROM `registration`", function
(err, result) {
DNSCHANNEL
(unknown stack trace)
GETADDRINFOREQWRAP
Update 2
When I put db.end()
inside setTimeout()
(without a provided delay number), setImmediate()
, or process.nextTick()
(in my main file), things work perfectly, no error is thrown, and the program ends normally. Although it works, I still didn't get to the root cause, so can someone tell me what happened?