Node.js mssql全局连接已存在连接池内存泄漏

时间:2017-06-29 18:50:29

标签: sql sql-server node.js

Node新手。我已经看了一些其他问题,但没有找到直接答案。我正在为一个简单的Sql数据调用创建一个模块,但是我收到“全局连接已存在”错误。我相信我的解决方案在于使用connectionPool,但我不确定如何创建连接池或者我是否正确地执行它。任何帮助表示赞赏。我也删除了任何敏感的登录数据。

更新:通过切换我的sql.close()并将其放在我的onDone&& onDone()回调函数之前,我现在收到了正确的数据,但是节点现在正在发出警告“警告:检测到可能的EventEmitter内存泄漏。添加了11个错误侦听器。使用emitter.setMaxListeners()增加限制”

什么行不通:我可以成功运行我的第一个电话,但之后每次通话都不会返回任何电话。我已经使用SQL分析器验证了只对服务器进行了一次调用。所以我相信我的代码中的某些东西绕过第二个调用并返回一个空结果集?

const self = {};
// NPM package that supports accessing Microsoft SQL databases from node.
let sql = require('mssql');
let debug = false;
let connection;
// configure SQL Connection. The credentials below should be handled more securely. 
// While this backend code is not accessible via public web accesses, it will be in
// GitHub.
let config = {
    domain:   "hidden",
    user:     "hidden",
    password: 'hidden',
    server:   'hidden', 
    database: 'hidden',

    options: {
        encrypt: false // Use this if you're on Windows Azure 
    }
};

self.getSQLData = function getSQLData (sprocInfo,onDone,onRow) {
    // Connect to SQL Server.
    if(debug) console.log("Hit SQL Function \n");
    if (typeof(sprocInfo.parameters) === 'object' && Array.isArray(sprocInfo.parameters)){
        if(debug)console.log("Passed array check");

        if(sprocInfo.database) config.database = sprocInfo.database;

        if(connection && connection._connected){
            if(debug) console.log("Inside SQL Connection");
            const request = new sql.Request(connection); //create a new request
            request.stream = true;
            var results = [];
            //put all parameters as input parameters to sql call
            sprocInfo.parameters.forEach(function(element){
                request.input(element.parameterName.toString(),sql.NVarChar,element.parameterValue);
                //NEED to remove hard typing of Integers. Maybe case statement based on typeof??
            });

            request.on('row', row => {
                results.push(row);
                onRow && onRow(row);
            });

            request.on('done', result => {
                // console.log("Result in request.on done"+JSON.stringify(result));
                onDone && onDone(results);
                sql.close();
            });

            request.on('error', err => {
                // May be emitted multiple times
                if ( debug ) console.log('error');
                if ( debug ) console.log('the error is: ' + err);
            });

            request.execute(sprocInfo.procedure)
        } else {
            connection = sql.connect(config, err => {
                if(debug) console.log("Inside SQL Connection");
                const request = new sql.Request(); //create a new request
                request.stream = true;
                var results = [];
                //put all parameters as input parameters to sql call
                sprocInfo.parameters.forEach(function(element){
                    request.input(element.parameterName.toString(),sql.NVarChar,element.parameterValue);
                    //NEED to remove hard typing of Integers. Maybe case statement based on typeof??
                });

                request.on('row', row => {
                    results.push(row);
                    onRow && onRow(row);
                });

                request.on('done', result => {
                    // console.log("Result in request.on done"+JSON.stringify(result));
                    onDone && onDone(results);
                    sql.close();
                });

                request.on('error', err => {
                    // May be emitted multiple times
                    if ( debug ) console.log('error');
                    if ( debug ) console.log('the error is: ' + err);
                });

                request.execute(sprocInfo.procedure)

            });
        }


        sql.on('error', err => {
            console.log("Error: Failed to connect to SQL Server");
        });

    } else {
        throw new Error ("Parameters is not an array");
    }
};

module.exports = self;

0 个答案:

没有答案