[英]How to reconnect SQL Server tedious when connection is lost
js和使用繁琐的连接。 连接成功,我可以连接。 但是在一段时间内,我会失去连接并在 Azure 中收到这些错误。
RequestError:请求只能在 LoggedIn state 中进行,而不是在 RequestError 的最终状态(D:\home\site\wwwroot\node_modules\tedious\lib\errors.js:32:12)
在 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 服务器?
您可以尝试使用以下代码重试连接:
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();
});
}
一旦建立了初始连接但同时丢失了,池将自动获取连接并处理您的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.