连接时不能关闭池?



我正试图在我的服务器代码中实现一些查询,但我面临这个错误:

setImmediate(callback, new ConnectionError('Cannot close a pool while it is connecting'))

ConnectionError: Cannot close a pool while it is connecting

我在网上查了一下,它说使用mssql.query而不是mssql.timeout,但我的代码都有,所以它让我很困惑。我试图增加设置超时从500到1500认为错误是因为这个,但仍然有崩溃。

一个奇怪的事情服务器并不总是崩溃。它只是在执行查询时随机发生

服务器发送信标及其所在楼层的数据。

我一直在试图弄清楚这个相当长的一段时间了。我很感激你的帮助。

下面是错误发生的部分:

// Handle POST requests for downloading files for beacons
app.post('/beacon', function (req, res) {
// POST variables
var auth = req.body.auth;
var gid = req.body.gid;
var fno = req.body.fno;
// Database Information 
const config = {
user: 'RANDOM',
password: auth,
server: 'localhost',
database: 'beacons',
"options": {
"encrypt": true,
"enableArithAbort": true
},
port: 1499
};
try{
// Connect to database
mssql.connect(config, function (err) {
// Create request handler and query for files
var request = new mssql.Request();
var query = `SELECT beacon_file FROM dbo.beacons WHERE group_id = ${gid} AND floor_num = ${fno}`;
// Query for file to download
request.query(query,
function (err, records) {
try{
if (err){
console.log(err);
throw err;
}
try {
// If there are files associated with the given group ID
if (records.rowsAffected != '0'){
// Get filename & send for download
// NOTE: We can either send a download or send text, not both using the current setup
var fileName = 'beaconfiles/' + records.recordset[0].beacon_file;
res.download(fileName, function (derr) {
if (derr) {
throw(derr);
}
else {
console.log(`Successfully sent ${fileName} for download.`)
}
});
}
else{
res.send(`<p>There are no files with that group ID.</p>
<a href="javascript:history.back()">Go Back</a>`);  
}
}
catch (err) {
console.log(`There was an error:  ${err} `);
res.send(`<p>There was an error. Make sure the group ID is correct: ${err}</p>
<a href="javascript:history.back()">Go Back</a>` );
}
}
catch(err){
res.send(`<p>There was an error getting the file: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
setTimeout(function(){
mssql.close();
}, 1500);
});
}
catch(err){
console.log(`There was an issue connecting to the database, check the authorization: ${err} `);
res.send(`<p>There was an issue connecting to the database, check the authorization: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
// Handle POST requests for downloading files for beacons
app.post('/floor', function (req, res) {
// POST variables
var auth = req.body.auth;
var gid = req.body.gid;
var fno = req.body.fno;
// Database Information 
const config = {
user: 'RANDOM',
password: auth,
server: 'localhost',
database: 'beacons',
"options": {
"encrypt": true,
"enableArithAbort": true
},
port: 1499
};
try{
// Connect to database
mssql.connect(config, function (err) {
// Create request handler and query for files
var request = new mssql.Request();
var query = `SELECT floor_file FROM dbo.beacons WHERE group_id = ${gid} AND floor_num = ${fno}`;
// Query for file to download
request.query(query,
function (err, records) {
try{
if (err){
console.log(err);
throw err;
}
try {
// If there are files associated with the given group ID
if (records.rowsAffected != '0'){
// Get filename & send for download
// NOTE: We can either send a download or send text, not both using the current setup
var fileName = 'beaconfiles/' + records.recordset[0].floor_file;
res.download(fileName, function (derr) {
if (derr) {
throw(derr);
}
else {
console.log(`Successfully sent ${fileName} for download.`)
}
});
}
else{
res.send(`<p>There are no files with that group ID.</p>
<a href="javascript:history.back()">Go Back</a>`);  
}
}
catch (err) {
console.log(`There was an error:  ${err} `);
res.send(`<p>There was an error. Make sure the group ID is correct: ${err}</p>
<a href="javascript:history.back()">Go Back</a>` );
}
}
catch(err){
res.send(`<p>There was an error getting the file: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
setTimeout(function(){
mssql.close();
}, 1000);
});
}
catch(err){
console.log(`There was an issue connecting to the database, check the authorization: ${err} `);
res.send(`<p>There was an issue connecting to the database, check the authorization: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});

当试图执行我的程序时,我也有同样的问题(我已经在MSSQL中编写了我的程序,我只需要使用exec调用它)。下面是我的Execute代码:

module.exports.Execute = (sSQL) => {
return new Promise((resolve, reject) => {
sql.connect(config, err => {
if (err)
reject(err);
var request = new sql.Request();
request.query(sSQL, (err, result) => {
if (err)
reject(err);
sql.close();
if (result)
resolve(result)
})
});
})
}

通过用户AlwaysLearning查看node-mssql,我只是删除了sql.close()并根据文档添加了await。为了确定,我定义了一个名为procedureQueryString的新函数:

procedureQueryString: (sSQL) => {
return new Promise(async (resolve, reject) => {
await sql.connect(config, async err => {
if (err)
reject(err);
var request = new sql.Request();
await request.query(sSQL, (err, result) => {
if (err)
reject(err);
if (result)
resolve(result)
})
});
})
}

它现在正在工作

相关内容

  • 没有找到相关文章

最新更新