NodeJS Mysql连接器:“池已关闭”

时间:2018-08-17 10:58:31

标签: javascript mysql node.js express

我们有一个NodeJS应用程序,它可以作为我们也在构建的前端Angular应用程序的API。

到目前为止,两种方法都运行良好,但是API有时会出现一个奇怪的错误问题:“池关闭”。现在,我正在处理需要执行很多操作的部分(例如,数据库中6-10次顺序插入和选择),此错误总是或几乎总是发生,这使我们遇到了一个大问题解决。

这是我们在DAO中使用的Mysqlconnector类:

import { environment } from "../environments/environment";

var mysql = require('mysql')

export class MysqlConnector {
    static pool: any;
    static host: any;
    static database: any;

    constructor() {

    }

    public static connect() {
        if (!MysqlConnector.pool) {
            MysqlConnector.pool = mysql.createPool({
                connectionLimit: 10,
                host: MysqlConnector.host,
                user: environment.mysql.data.user,
                password: environment.mysql.data.password,
                database: MysqlConnector.database
            });
        }
      //  console.log("pool: ", MysqlConnector.pool)
        return MysqlConnector.pool;
    }

    /**
     * This method can be used to directly perform a SQL query to the monitoring DB.
     * @param query SQL query string
     */
    public static async monitoringDatabaseQuery(query) {
        return new Promise((resolve, reject) => {
            const connection = mysql.createPool({
                connectionLimit: 10,
                host: 'our-host',
                user: environment.mysql.data.user,
                password: environment.mysql.data.password,
                database: 'monitoring'
            });

            connection.query(query, (err, data) => {
                if (err) reject(err);
                connection.end();
                resolve(data);
            });
        })
    }

    public static setupDB(req, res, next) {
       // method that we use for picking up the correct DB at API call init
    }

    public static end() {
        const pool = MysqlConnector.connect();
        pool.end();
        MysqlConnector.pool = null;
    }

    public static query(queryString: string) {
        return new Promise((resolve, reject) => {
            const pool = MysqlConnector.connect();

            pool.query(queryString, (err, result) => {
                if (err) reject(err);
                resolve();
            });
        });
    }

    public static select(queryString: string) {
        return new Promise<any[]>((resolve, reject) => {
            const pool = MysqlConnector.connect();

            pool.query(queryString, (err, result, fields) => {
                if (err) reject(err);
                resolve(result);
            });
        });
    }
}

我的“ mysql”软件包版本:

“ mysql”:“ ^ 2.15.0”,

当我执行我之前谈到的特定操作时,我会遇到很多错误:

  

错误:池已关闭。       在Handshake.onConnect上[作为_callback](C:\ Zerok \ Dev \ WorkProjects \ api \ node_modules \ mysql \ lib \ Pool.js:52:15)       在Handshake.Sequence.end(C:\ Zerok \ Dev \ WorkProjects \ api \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:88:24)       在C:\ Zerok \ Dev \ WorkProjects \ api \ node_modules \ mysql \ lib \ protocol \ Protocol.js:398:18       在Array.forEach()       在C:\ Zerok \ Dev \ WorkProjects \ api \ node_modules \ mysql \ lib \ protocol \ Protocol.js:397:13       在_combinedTickCallback(内部/进程/next_tick.js:131:7)       在process._tickCallback(内部/进程/next_tick.js:180:9)       --------------------       在Pool.query(C:\ Zerok \ Dev \ WorkProjects \ api \ node_modules \ mysql \ lib \ Pool.js:199:23)       在Promise(C:\ Zerok \ Dev \ WorkProjects \ api \ dist \ lib \ MysqlConnector.js:91:18)       在新的Promise()       在Function.query(C:\ Zerok \ Dev \ WorkProjects \ api \ dist \ lib \ MysqlConnector.js:89:16)       在功能。 (C:\ Zerok \ Dev \ WorkProjects \ api \ dist \ dao \ EventDAO.js:146:63)       在Generator.next()       在C:\ Zerok \ Dev \ WorkProjects \ api \ dist \ dao \ EventDAO.js:7:71       在新的Promise()       在__awaiter(C:\ Zerok \ Dev \ WorkProjects \ api \ dist \ dao \ EventDAO.js:3:12)       在selectedDimensions.forEach(C:\ Zerok \ Dev \ WorkProjects \ api \ dist \ dao \ EventDAO.js:143:65)中的代码:   'POOL_CLOSED'}

这是发生代码的那段代码,但是正如我之前说过的,它有时完全在API的其他部分发生:

private static async changeEffectDimensions(dimensionsList, eventEffectID) {
    if (dimensionsList.length > 0) {
        try {
            const dimensionsModel = new Dimensions();
            const dimensions = await dimensionsModel.getAll();
            const selectedDimensions = dimensionsModel.filterDimensions(dimensions, dimensionsList);
            let sql;

            await selectedDimensions.forEach(async dimension => {
                sql = `INSERT IGNORE INTO EffectDimensions (event_effect_id, dimension_id) VALUES (${eventEffectID}, ${dimension.dimension_id})`;
                await MysqlConnector.query(sql);
            });
            return true;
        } catch (err) {
            console.log("ERROR: ", err)
            throw err;
        }
    }
}

可能是什么原因造成的?谢谢!

0 个答案:

没有答案