提问者:小点点

如何避免MySQL中的最大连接错误?


这种情况发生得非常频繁(每周一次,大约30-40分钟),当我试图通过heidisql连接时,数据库突然提到最大连接数,并且任何API调用都响应以下错误:

Cannot read property 'release' of undefined

我在MySQL中的每个查询之后调用。release()。我是不是遗漏了什么,我是不是应该叫。结束呢?我将nodejs与MySQL一起使用。

下面是我包装每个查询和池代码的方法:

var mysql = require('mysql');

var mysql_pool = mysql.createPool({
    connectionLimit: config.mysql.limit,
    host: config.mysql.host,
    user: config.mysql.user,
    password: config.mysql.pw,
    database: config.mysql.db //,
    // debug: true
});
var qSelect = "SELECT id FROM Users";
    var qValues = [];
    var qCall = mysql.format(qSelect, qValues);
    mysql_pool.getConnection(function(err_pool, connection) {
        if (err_pool) {
            connection.release();
            console.log(' Error getting mysql_pool connection: ' + err_pool);
            throw err_pool;
        }
        connection.query(qCall, function(err, userFound, fields) {
            connection.release();
            if (err) {
                console.log("get user : " + err);
            } else {
               //some code here
            }

        });

能不能请人指点一下,谢谢。


共1个答案

匿名用户

应删除if循环中使用的第一个connection.release()

if (err_pool) {
            console.log(' Error getting mysql_pool connection: ' + err_pool);
            throw err_pool;
        }