【问题标题】:How to reconnect SQL Server tedious when connection is lost连接丢失时如何重新连接 SQL Server 很乏味
【发布时间】:2022-01-20 16:19:14
【问题描述】:

js 和使用繁琐的连接。连接成功,我可以连接。但是在一段时间内,我会失去连接并在 Azure 中收到这些错误。

RequestError:请求只能在 LoggedIn 状态下进行,不能在 RequestError (D:\home\site\wwwroot\node_modules\tedious\lib\errors.js:32:12) 的 Final 状态下进行

在 Connection.makeRequest (D:\home\site\wwwroot\node_modules\tedious\lib\connection.js:2380:49) 在 Connection.execSql (D:\home\site\wwwroot\node_modules\tedious\lib \connection.js:2034:10) at submitCode (D:\home\site\wwwroot\controller\mssqlController.js:121:16) at Layer.handle [as handle_request] (D:\home\site\wwwroot\node_modules \express\lib\router\layer.js:95:5) at next (D:\home\site\wwwroot\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D: \home\site\wwwroot\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (D:\home\site\wwwroot\node_modules\express\lib\router\layer .js:95:5) 在 D:\home\site\wwwroot\node_modules\express\lib\router\index.js:281:22at Function.process_params (D:\home\site\wwwroot\node_modules\express\lib \router\index.js:335:12) {code: 'EINVALIDSTATE'}[0mPOST /login [32m200 [0m11.820 ms - 27[0m

这是我的代码:

let config = {
    server: '',
    authentication: {
        type: 'default',
        options: {
            userName: '',
            password: ''
        }
    },
    options: {
        // If you are on Microsoft Azure, you need encryption:
        encrypt: true,
        database: ''
    }
};

let connection = new Connection(config);
connection.on('connect', function (err) {
    // If no error, then good to proceed.  
    console.log("err", err);
});

let Request = require('tedious').Request
let TYPES = require('tedious').TYPES;

我的问题是,如果连接丢失,我可以在哪里添加什么来重新连接或重置 Azure 中的 SQL Server?

【问题讨论】:

标签: node.js sql-server azure


【解决方案1】:

您可以尝试使用以下代码重试连接:

let intervalFunction;
const INTERVAL_DURATION = 4000;

if (require.main === module){
    console.log("Listening on http://localhost:" + config.port + " ...");
    app.listen(config.port);
    // try to connect to db and fire main on succes. 
    intervalFunction = setInterval(()=> getConnection(), INTERVAL_DURATION);
}

function getConnection() {
  sql.close();
  sql.connect(config.database).then(() => {
    sql.close();
    clearInterval(intervalFunction);
    main();
}).catch(function(err) {
    console.error(err);
    console.log(`DB connection will be tried again in ${INTERVAL_DURATION}ms`)
    sql.close();
  });
}

一旦建立了初始连接但同时丢失了,那么池将自动拾取连接并处理您的连接。

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2020-09-21
    • 2012-03-27
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多