node.js tedious ConnectionError: Failed to connect to sqlserverip:1433 crashing express server

node.js tedious ConnectionError: Failed to connect to sqlserverip:1433 crashing express server

我无法捕捉到这个 node.js 乏味的错误 ConnectionError: Failed to connect to sqlserverip:1433,它使我的 Express 服务器意外崩溃。谁能建议我应该怎么做才能避免此类崩溃?

tedious@1.2.2
tedious-connection-pool@0.3.2
node --version v0.10.33

我的繁琐程序在这里:

// mssql.js

// mssql.js
"use strict";

// Import modules
var path = require('path');
var async = require('async');
var fs = require('fs');
var ConnectionPool = require('tedious-connection-pool');
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

// Export module with Namespace
var swmssqlc = exports;

// Constants
var DEFAULT_CONNLIMIT_MAX = 30;
var DEFAULT_CONNLIMIT_MIN = 0;
var DEFAULT_USEUTC = true;
var DEFAULT_REQUESTTIMEOUT = 15000;
var DEFAULT_CONNTIMEOUT = 15000;

/**
 * MSSQL Class Wrapper for SolarWinds
 * @param {object} connProp: this contains all the following connection properties
 * @param {string} connProp.user: user of database account
 * @param {string} connProp.password: password for database account
 * @param {string} connProp.database: database name
 * @param {string} connProp.instanceName: instance name
 * @param {string} connProp.host: hostname of server
 * @return {object} object
 */
swmssqlc.SWDBClient = function SWDBClient(connProp) {
    this.user = connProp.user;
    this.password = connProp.password;
    this.database = connProp.database;
    this.instanceName = connProp.instanceName;
    this.connectTimeout = connProp.connectTimeout || DEFAULT_CONNTIMEOUT;
    this.requestTimeout = connProp.requestTimeout || DEFAULT_REQUESTTIMEOUT;
    this.useUTC = connProp.useUTC || DEFAULT_USEUTC;
    this.host = connProp.host;
    this.connectionLimitMax = connProp.connectionLimitMax || DEFAULT_CONNLIMIT_MAX
    this.connectionLimitMin = connProp.connectionLimitMin || DEFAULT_CONNLIMIT_MIN
    this.pool = null;

    this.connProp = {
            server          : this.host,
            userName        : this.user,
            password        : this.password,
            options         : {
                                database        : this.database,
                                instanceName    : this.instanceName,
                                connectTimeout  : this.connectTimeout,
                                requestTimeout  : this.requestTimeout,
                                useUTC          : this.useUTC
                              }
    };

    this.poolConf = {
                        min : this.connectionLimitMin,
                        max : this.connectionLimitMax,
    };
};

swmssqlc.SWDBClient.prototype.connect = function connect(callback) {
    var pool = new ConnectionPool(this.poolConf, this.connProp);
    this.pool = pool;

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

    pool.on('error', function(err) {
        console.error('ERROR pool-connecting to swmssqlc server with connProp:', this.connProp);
        return callback(err);
    });
};

swmssqlc.SWDBClient.prototype.disconnect = function disconnect(callback) {
    var pool = this.pool;
    console.log('disconnecting swmssqlc');
    pool.drain(callback);
};

swmssqlc.SWDBClient.prototype.runQry = function runQry(qry, args, callback) {
    /**
     * run query
     * @param {string} qry: sql query
     * @param {object} args: object as args.params = {'paramName': {paramValue: 1, paramType: tedious.TYPES.Type}}
     * @param {Number} callback(err, port) function
     * @return callback return
     */
    // console.log('running swmssqlc qry:', qry);  // log for (testing) only
    this.pool.acquire(function(err, connection) {
        if (err) {
            console.error('swmssqlc-runQry-cannot acquire ERROR:', err);
            return callback(err);
        }
        connection.on('error', function(err) {
            console.error('ERROR executing query on swmssqlc server due to connection error');
            return callback(err);
        });
        var rows = [];
        var request = new Request(qry, function(err, rowCount) {
            if (err) {
                console.error('swmssqlc-runQry-request ERROR:', err);
                connection.release();
                return callback(err);
            } else {
                connection.release();
                return callback(null, rows);
            }

        });

        request.on('row', function(columns) {
            rows = columns
        });

        if (args && args.params) {
            for (var paramName in args.params) {
                request.addParameter(paramName.toString(), args.params[paramName].paramType, args.params[paramName].paramValue);
            }
        }
        connection.execSql(request);
    });
};

//错误在这里

swmssqlc-runQry-request ERROR: { [RequestError: Timeout: Request failed to complete in 15000ms]
  name: 'RequestError',
  message: 'Timeout: Request failed to complete in 15000ms',
  code: 'ETIMEOUT' }
swmssqlc-runQry-request ERROR: { [RequestError: Timeout: Request failed to complete in 15000ms]
  name: 'RequestError',
  message: 'Timeout: Request failed to complete in 15000ms',
  code: 'ETIMEOUT' }
ConnectionError: Failed to connect to sqlserverip:1433 in 15000ms
    at Connection.connectTimeout (/home/amit/workspace/myproject/node_modules/tedious/lib/connection.js:628:26)
    at null._onTimeout (/home/amit/workspace/myproject/node_modules/tedious/lib/connection.js:3:59)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

我发现我的代码有问题...这是因为我在 swmssqlc.connect() 的回调错误中有一个 throw 异常,它在 pool.on 引发时被调用一个例外。