Nodejs Mysql lib错误“连接太多”

时间:2018-07-11 17:02:09

标签: mysql node.js express connection-pooling mysqljs

我使用的库:mysqljs / mysql

我在mysql服务器上有250个最大连接限制,但是我的应用程序有时会显示“连接过多”。

当我检查时,使用show full processlist;命令,mysql服务器只有3个连接

我在代码中做错了吗?

app / config / database.json

{
    "production": {
        "db1": {
            "multipleStatements": true,
            "host": ****,
            "user": ****,
            "password": ****,
            "database": ****,
            "connectionLimit": 190,
            "port": 3306
        }
}

app / models / index.js

const mysql = require('mysql')
const config = require('../config/database')

const db1 = mysql.createPool(config[process.env.APP_ENV].db1)
const db2 = mysql.createPool(config[process.env.APP_ENV].db2)
const db3 = mysql.createPool(config[process.env.APP_ENV].db3)
const db4 = mysql.createPool(config[process.env.APP_ENV].db4)

const connections = {
    'db1': db1,
    'db2': db2,
    'db3': db3,
    'db4': db4
}

// Models
const News = require('./News')
const Programs = require('./Programs')

module.exports = {
    News: new News(connections),
    Programs: new Programs(connections)
}

app / models / News.js

class News {
    constructor(connections) {
        this.db = connections;
    }

    getNews(limit, offset) {
        return new Promise((resolve, reject) => {
            this.db.db1.getConnection((err, db1) => {
                if (err) {
                    console.log(err)
                    db1.destroy()
                    reject(err)
                }

                db1.query(query, (err, rows) => {
                    if (err) {
                        console.log(err)
                        db1.destroy()
                        reject(err)
                    }
                    db1.destroy()
                    resolve(rows)
                });
            });
        }); 
    }
}
module.exports = News;

app / controllers / NewsController.js

const models = require('../models/index')

class NewsController {

    index (req, res) {
        models.News.getNews(limit, offset).then((result) => {
            res.status(200).send(result);
        }).catch((err) => {
            res.status(503).send(err);
        });
    }

    // more functions that can perform 2 or 3 queries consecutives 
    // with differents model functions.
}
module.exports = NewsController

app / controllers / index.js

const NewsController = require('./NewsController')

module.exports = {
    NewsController: new NewsController()
    // more controllers...
}

app / routes.js

const express = require('express')
const router = express.Router()
const controllers = require('./controllers')

router.get('/news', (req, res) => {
    controllers.NewsController.index(req, res)
})
// more routes with newscontroller...

1 个答案:

答案 0 :(得分:0)

The documentation状态:

  

pool.getConnectionconnection.queryconnection.release

您不是在呼叫release,而是在呼叫destroy,这会立即终止连接,而不是将其释放回池中。因此,您的池中充满了新的连接。

此外,鉴于仅使用池化连接来运行单个查询的方式,您可以使用文档中也提到的快捷方式:

this.db.db1.query(query, (err, rows) => {
  if (err) {
    console.log(err)
    reject(err)
  } else {
    resolve(rows)
  }
});

这样,所有池管理都是自动完成的。