我试图在我的服务器代码中实现一些查询,但我面临这个错误:
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
但我的代码两者都有,所以这让我很困惑。我试图将 set 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
调用它)。这是我的执行代码:
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)
})
});
})
}
现在开始工作了