gpt4 book ai didi

node.js - 连接丢失时如何重新连接 SQL Server 乏味

转载 作者:行者123 更新时间:2023-12-03 03:42:47 35 4
gpt4 key购买 nike

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

RequestError: Requests can only be made in the LoggedIn state, not the Final stateat RequestError (D:\home\site\wwwroot\node_modules\tedious\lib\errors.js:32:12)

at Connection.makeRequest (D:\home\site\wwwroot\node_modules\tedious\lib\connection.js:2380:49)at 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)at Layer.handle [as handle_request] (D:\home\site\wwwroot\node_modules\express\lib\router\layer.js:95:5)at 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?

最佳答案

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

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();
});
}

一旦建立初始连接但在此期间丢失,池将自动拾取连接并处理您的连接。

关于node.js - 连接丢失时如何重新连接 SQL Server 乏味,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70398264/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com