I am working in a proyect with nodejs + mysql2 and some times I got the error Too many connections.
My connection:
import {createPool, Pool} from 'mysql2/promise';
export async function connect(): Promise < any > {
const connection: Pool = await createPool({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'mendozarq',
connectionLimit: 10
});
return connection;
}
Controller:
import { Response, Request } from 'express';
import { FieldPacket, Pool } from 'mysql2/promise';
import { connect } from './../../classes/database';
import { Personal } from './../../models/personal.interface';
export const getAllPersonal = async (req: Request, res: Response) => {
try {
const conn: Pool = await connect();
const [personal]: [any[], FieldPacket[]] = await conn.query('SELECT * FROM personal ORDER BY creadoEn DESC');
conn.end();
return res.status(200).json(personal);
} catch (error) {
return res.status(400).json({
error
});
}
}
So, my question is I have to use createConnection instead of createPool or I just have to create one instance of my conn.