简体   繁体   中英

node-mysql pool active connections not getting released?

I don't know why, but even after calling connection.release() inside pool.getConnection , the no. of connections keeps increasing instead of reusing idle connections? Does anyone know if I am doing anything wrong?

I am using node-mysql (mysql@2.5.4) on Ubuntu.

I am basically calling pool.getConnection every second in this testcase (simulating web clients requesting triggering pool requests) and also calling connection.release() within it. Even then, the no. of connections to server keeps increasing (I see that by giving command watch -n 2 "netstat -an | grep 127.0.0.1.*3306.*ESTABLISHED | wc -l" as mysql server is local) until I hit enqueue event telling me to wait for connections.

Here is my test:

/**
 * 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);
    });
}
   if (err) {//do this if you get err
            connection.release();
            console.error('runQry-cannot run qry ERROR:', err);
            return callback(err);
        }

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