Using the mysql2/promise module, I'm creating a connection pool like this:
import mysql from 'mysql2/promise';
async function getConnection() {
let pool = await mysql.createPool({
connectionLimit: 10,
host:'localhost',
user: 'root',
database: 'customers'
});
return pool.getConnection();
}
export {
getConnection
};
In another file, I am access this functionality like this:
import {getConnection} from '../config/database/mysql';
async function getCustomerAddresses(id){
let conn = await getConnection();
let [rows, fields] = await conn.execute(
`SELECT *
FROM addresses WHERE customer = ?`, [id]);
conn.release();
return rows;
}
After calling the function a couple of times, I'm getting the following error:
Too many connections
What am I doing wrong? And is there a more elegant way so I don't have to include the getConnection() call in every function?
Based on my experience, I believe that your problems is this code
In my case, I changed it to
I just read the documentation and I think the difference is explained in the documentation by this:
Whereas destroy is this
Hope this helps.