0

我在我的项目中使用 mssql 节点模块,在我不进行任何异步调用之前一切都很好。

在多个异步调用中,它的开始给我连接错误“连接已关闭”。有人可以查看代码并帮助我。

==== SqlServerDAL.js 是这样的

 var sql = require('mssql');
 var _sqlConfig = require("../../../../config/dbconfig.json");

function SqlServerDAL() {
    this._sqlConfig = _sqlConfig;
    this._sqlConnection = null;
}

/**
 * @param : request > request or query parmanter to excute sql query
 * @param : callback > after execution neect to send response back to serr  
 * @param : sqlReqConf > to build the requst object from the configuration
 */
SqlServerDAL.prototype.executeRequest = function (request, callback) {

    var self = this;

    self._sqlConnection = new sql.Connection(this._sqlConfig);

    this._sqlConnection.connect().then(function () {

        var _sqlRequest = new sql.Request(self._sqlConnection);

        try {
            _sqlRequest.input('someVar','someValue')

            _sqlRequest.execute('myStoreProc').then(function (recordsets) {
                callback(null, recordsets);
            }).catch(function (sqlEx) {
                callback({
                    code: "ERR0003",
                    message: sqlEx.message,
                    type: sqlEx.name
                }, null);
            });
        } catch (sqlEx) {
            callback({
                code: "ERR0004",
                message: sqlEx.message,
                type: sqlEx.name
            }, null);
        }

    }).catch(function (sqlEx) {
        callback({
            code: "ERR0001",
            message: sqlEx.message,
            type: sqlEx.name
        }, null);
    });
};

module.exports = new SqlServerDAL();

我从 dal.js 调用的这个以上文件

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ /*global require, module,logger */

    var Q = require("q");
    var sqlServerDAL = require('./sqlDAL/sqlServerDAL');

    /**
     * 
     * @constructor:  
     */

    function DAL() {}

    /**
     * @param : customRequst : this json object will come from respective dbschema files,
     *                       This object bening used to build sqlRequest object
     */

    DAL.prototype.executeRequest = function (customRequest) {

        var deferred = Q.defer();

        sqlServerDAL.executeRequest(customRequst, function (sqlEx, recordsets) {

            if (recordsets) {
                logger.info("_sqlRequest is successfully executed:" + JSON.stringify(recordsets));
                deferred.resolve(recordsets);
            } else if (sqlEx) {
                deferred.reject({
                    code: sqlEx.code,
                    message: sqlEx.message,
                    type: sqlEx.name
                });
            }
        });

        return deferred.promise;
    };


    module.exports = new DAL();

// 我的控制器就像

 app.use("mycontroller", function(req, res, next){
 var dal = new Dal();
 dal.executRequest(function(result){
     res.send(result)
 } , function(err){
   console.log("got some error" + errr)
 })
  })
4

1 回答 1

0

是的,我得到了答案,由于异步调用新请求(self.connection)正在获取旧对象,该对象被旧函数调用关闭。所以答案是我们需要像这样创建新请求。

this._sqlConnection.connect().then(function (connection ) {
  var _sqlRequest = new sql.Request(connection);
})
于 2016-04-12T10:16:43.043 回答