nodejs mssql中的SQL Server连接超时错误

时间:2018-01-11 11:41:10

标签: sql-server node.js sql-server-2016 tedious node-mssql

执行几个SQL语句后总是会出现以下错误。

我正在使用

var sql = require("mssql");
  

(node:12240)[DEP0064] DeprecationWarning:tls.createSecurePair()是   弃用。请改用tls.Socket。 warning.js:18(node:12240)   UnhandledPromiseRejectionWarning:未处理的承诺拒绝   (rejection id:2):ConnectionError:连接失败   Server-100:在15000ms中未定义warning.js:18(节点:12240)[DEP0018]   弃用警告:已弃用未处理的承诺拒绝。在   未来,未经处理的承诺拒绝将终止   Node.js使用非零退出代码进行处理。 warning.js:18(node:12240)   UnhandledPromiseRejectionWarning:未处理的承诺拒绝   (rejection id:4):ConnectionError:连接失败   Server-100:在15000ms中未定义warning.js:18

ConnectionError: Failed to connect to server-100:undefined in 15000ms
 at Connection.tedious.once.err (d:\API\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:316:30)
    at emitOne (events.js:115:13)
    at Connection.emit (events.js:210:7)
    at Connection.connectTimeout (d:\API\node_modules\tedious\lib\connection.js:634:12)
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5)

以下是代码:

module.exports = function (data) {

    return new Promise(function (resolve, reject) {

        var database = new sql.ConnectionPool({
        user: 'sa',
        password: 'password',
        server: 'Server-100',
        database: 'SQLDB',
        options: {
            encrypt: true,
            useUTC: true
        },
        pool: {
            max: 10,
            min: 0,
            idleTimeoutMillis: 60000
        }
    });

        database.connect().then(function () {

            var req = new sql.Request(database);
            req.input(data.parameter.Name, data.parameter.DataType, data.parameter.Value);                
            req.execute(data.procedureName).then(function (result) {
                database.close();
                return resolve(result);
            }).catch(function (err) {
                database.close();
                return reject(err);
            });

        }).catch(function (err) {
            database.close();
            return reject(err);
        });
    });
}
  

发生异常:ConnectionError ConnectionError:失败   连接到10.0.2.183:undefined在15000ms       在Connection.tedious.once.err(d:\ Projects \ node_modules \ mssql \ lib \ tedious.js:216:17)       at Object.onceWrapper(events.js:316:30)       在emitOne(events.js:115:13)       在Connection.emit(events.js:210:7)       在Connection.connectTimeout(d:\ Projects \ node_modules \ tedious \ lib \ connection.js:634:12)       在ontimeout(timers.js:469:11)       在tryOnTimeout(timers.js:304:5)       在Timer.listOnTimeout(timers.js:264:5)

我是连接池和节点js mssql库的新手。

请在此提出建议。

1 个答案:

答案 0 :(得分:0)

在服务器位置使用本地主机并尝试。

var database = new sql.ConnectionPool({
        user: 'sa',
        password: 'password',
        port:"1433",
        server: 'localhost',    //if your connecting to localhost\instance make sure you have the service 'SQL Server Browser' running. 
        database: 'SQLDB',
        options: {
            encrypt: true,
            useUTC: true
        },
        pool: {
            max: 10,
            min: 0,
            idleTimeoutMillis: 60000
        }
    });