简体   繁体   中英

Nodejs Mysql lib error “Too many connections”

Libary that I use: mysqljs/mysql

I have 250 max connections limit on mysql server, but my application sometimes get "Too many connections".

When I checked, mysql server only have 3 connections using show full processlist; command

Am I doing something wrong in my code?

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...

The documentation states:

pool.getConnectionconnection.queryconnection.release

You're not calling release but destroy , which immediately terminates the connection, instead of releasing it back to the pool. So your pools fill up with new connections.

Also, given how you only use the pooled connection to run a single query, you can use the shortcut also mentioned in the documentation:

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

That way, all the pool management is being done automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM