I have following setup,
db.js
const mysql = require("mysql");
const config = require("../core/config/config.json");
const db = mysql.createPool({
host: config.mysql.host,
port: config.mysql.port,
user: config.mysql.user,
password: config.mysql.password
});
db.getConnection((err, connection) => {
if (err) {
console.error(err);
return;
}
connection.ping();
connection.release();
console.log("Database connected !");
});
module.exports = db;
Then I have many query files as shown below,
query1.js
const mysql = require("../db");
mysql.query("select * from table", [param], (error, results) => {
if (error) {
return cb(error);
}
return cb(null, results);
});
query2.js
const mysql = require("../db");
mysql.query("select * from table", [param], (error, results) => {
if (error) {
return cb(error);
}
return cb(null, results);
});
query3.js
const mysql = require("../db");
mysql.query("select * from table", [param], (error, results) => {
if (error) {
return cb(error);
}
return cb(null, results);
});
and so on...
And I get
ER_CON_COUNT_ERROR: Too many connections error in node-mysql
When I deploy this code to AWS-lambda function. (Locally, it works fine).
What should I do it avoid above error with above setup ?
What am I doing wrong ?