Nodejs + mysql2/promise 连接太多
Too many connections Nodejs + mysql2/promise
我在使用 nodejs + mysql2 的项目中工作,有时我收到错误 太多连接。
我的联系人:
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;
}
控制器:
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
});
}
}
所以,我的问题是我必须使用 createConnection 而不是 createPool 或者我只需要创建一个我的 conn 实例。
您应该只调用 createPool
一次,然后每次都从池中调用 select。
所以,它应该看起来像这样。创建一个存储池的常量。然后在函数中如果池已经存在return 池而不调用createPool
。如果它不存在,那么您将调用 createPool
和 return 已创建的池。
import {createPool, Pool} from 'mysql2/promise';
const globalPool: Pool | undefined = undefined;
export async function connect(): Promise <Pool> {
// If the pool was already created, return it instead of creating a new one.
if(globalPool) {
return globalPool;
}
// If we have gotten this far, the pool doesn't exist, so lets create one.
globalPool = await createPool({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'mendozarq',
connectionLimit: 10
});
return globalPool;
}
我在使用 nodejs + mysql2 的项目中工作,有时我收到错误 太多连接。
我的联系人:
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;
}
控制器:
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
});
}
}
所以,我的问题是我必须使用 createConnection 而不是 createPool 或者我只需要创建一个我的 conn 实例。
您应该只调用 createPool
一次,然后每次都从池中调用 select。
所以,它应该看起来像这样。创建一个存储池的常量。然后在函数中如果池已经存在return 池而不调用createPool
。如果它不存在,那么您将调用 createPool
和 return 已创建的池。
import {createPool, Pool} from 'mysql2/promise';
const globalPool: Pool | undefined = undefined;
export async function connect(): Promise <Pool> {
// If the pool was already created, return it instead of creating a new one.
if(globalPool) {
return globalPool;
}
// If we have gotten this far, the pool doesn't exist, so lets create one.
globalPool = await createPool({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'mendozarq',
connectionLimit: 10
});
return globalPool;
}