node-mysql池活动连接没有被释放?

时间:2015-02-18 02:55:17

标签: mysql node.js connection-pooling

我不知道为什么,但即使在connection.release()内拨打pool.getConnection之后,也没有。连接不断增加而不是重用空闲连接?有谁知道我做错了什么?

我在Ubuntu上使用node-mysql(mysql@2.5.4)。

我基本上在此测试用例中每秒调用pool.getConnection(模拟请求触发池请求的Web客户端)并在其中调用connection.release()。即便如此,没有。与服务器的连接不断增加(我看到通过给出命令watch -n 2 "netstat -an | grep 127.0.0.1.*3306.*ESTABLISHED | wc -l",因为mysql服务器是本地的),直到我点击enqueue事件告诉我等待连接。

这是我的测试:

/**
 * Test mysql_client (./test.js)
 */
'use strict';
var async = require('async');
var mysqlc = require('../mysql_client');
var connProp = {
        connectionLimit : 10,
        host            : 'xxx',
        user            : 'xxx',
        password        : 'xxx',
        database        : 'xxx'
};
var mydbc = new mysqlc.MySqlClient(connProp);

async.series([
    function(callback) {
        mydbc.connect(callback);
    },
    function(callback) {
        var intervalObject;
        intervalObject = setInterval(function() {
            mydbc.runQry('SELECT 1', function(err, results) {
                if (err) {
                    console.error('foo-err:', intervalObject, err);
                    callback(err);
                } else {
                    console.log('resultsxxxx', results)
                }
            })
        }, 1000*0.5)
    },
    function(callback) {
        mydbc.disconnect(callback);
    }
    ],
    function(err, results) {
        console.log('results', results[1]);
});

// This results in the following output, finally hitting enqueue event:
connecting mysql
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
results undefined
runQry-Waiting for available connection slot-ERROR
running mysql qry: SELECT 1
runQry-Waiting for available connection slot-ERROR
running mysql qry: SELECT 1
runQry-Waiting for available connection slot-ERROR



// This is the wrapper for node-mysql (pretty straightforward)
/**
 * mysql_client (./mysql_client.js)
 */

// Import modules
var mysql = require('mysql');
var path = require('path');
var fs = require('fs');
var async = require('async');

// Export module with Namespace
var mysqlc = exports;

// Constants
var DEFAULT_MYSQL_PORT = 3306;
var DEFAULT_AUTOCOMMIT = false;
var DEFAULT_CONNLIMIT = 10;

/**
 * MySQL Class Wrapper
 * @param {string} user: user of database account
 * @param {string} password: password for database account
 * @param {string} database: database name
 * @param {string} host: hostname of server
 * @param {string} port: port of server
 * @param {boolean} autocommit: autocommit
 * @return {object} object
 */
mysqlc.MySqlClient = function MySqlClient(connProp) {
    "use strict";
    this.user = connProp.user;
    this.password = connProp.password;
    this.database = connProp.database;
    this.host = connProp.host;
    this.port = connProp.port || DEFAULT_MYSQL_PORT;
    this.autocommit = connProp.autocommit || DEFAULT_AUTOCOMMIT;
    this.connectionLimit = connProp.connectionLimit || DEFAULT_CONNLIMIT
    this.pool = null;

    this.connProp = {
            connectionLimit : this.connectionLimit,
            host            : this.host,
            port            : this.port,
            user            : this.user,
            password        : this.password,
            database        : this.database,
            multipleStatements: true
    };
}


mysqlc.MySqlClient.prototype.runQry = function runQry(qry, args, callback) {
    console.log('running mysql qry:', qry);
    this.pool.getConnection(function(err, connection) {
        if (err) {
            console.error('runQry-cannot getConnection ERROR:', err);
            return callback(err);
        }
        connection.query(qry, args, function(err, rows) {
            if (err) {
                console.error('runQry-cannot run qry ERROR:', err);
                return callback(err);
            }
            connection.release();
            return callback(null, rows);
        });
    });
};

mysqlc.MySqlClient.prototype.connect = function connect(callback) {
    var pool = mysql.createPool(this.connProp);
    this.pool = pool;

    pool.on('enqueue', function () {
        console.error('runQry-Waiting for available connection slot-ERROR');
        return callback('pool-not-empty');
    });

    // verify connection
    pool.getConnection(function(err, connection) {
        console.log('connecting mysql');
        if (err) {
            console.error('ERROR connecting to mysql server with connProp:', this.connProp);
            return callback(err);
        }
        connection.release();
        return callback(null);
    });
};

mysqlc.MySqlClient.prototype.disconnect = function disconnect(callback) {
    var pool = this.pool;
    pool.getConnection(function(err, connection) {
        console.log('disconnecting mysql');
        if (err) {
            console.error('ERROR disconnecting to mysql server with connProp:', this.connProp);
            return callback(err);
        }
        connection.release();
        pool.end();
        return callback(null);
    });
}

1 个答案:

答案 0 :(得分:0)

   if (err) {//do this if you get err
            connection.release();
            console.error('runQry-cannot run qry ERROR:', err);
            return callback(err);
        }