mssql节点模块中的连接错误

时间:2016-04-12 07:32:30

标签: node.js express node-mssql

我在我的项目中使用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)
 })
  })

1 个答案:

答案 0 :(得分:0)

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

this._sqlConnection.connect().then(function (connection ) {
  var _sqlRequest = new sql.Request(connection);
})