my goal is to automatically release the database connection after each query.
the following is the code I wrote, but when I called the interface, I reported an error Connection already released
how should I modify the code?
app.all("/resume/select", (req, res) => {
let selectSql = "SELECT id, company, position, salary, content, dateline from work_experience w where w.userID=? and w.id=?"; //ID
let loginUserInfo = req.session.loginUserInfo;
let param = req.body;
if(loginUserInfo === undefined) {
res.json({
code: "0000",
message: ""
})
} else {
// console.log(" loginUserInfo == ", loginUserInfo);
pool.getConnection( (err, connection) => {
if (err) {
throw err;
} else {
connection.query(
selectSql,
[loginUserInfo.uid, param.id],
function (err, rows) {
//console.log(result);
//res.json(result);
if (err) {
throw err;
} else {
// console.log("loginUserInfo ==", loginUserInfo);
let returnData = {
code: "0001",
data: rows
};
res.json(returnData);
}
connection.release();
}
)
}
})
}
});
app.all("/index/message/selectAll", (req, res) => {
let selectAdvertiseSql = "SELECT id, title, userID, salary, education, company, workPlace, dateline from advertise_message order by id DESC LIMIT ?, ?"; //ID
let selectArticleSql = "SELECT id, title, dateline from article order by id DESC LIMIT ?, ?";
let selectTenantSql = "SELECT id, title, dateline from tenant_message order by id DESC LIMIT ?, ?";
let param = req.body;
let selectParams = {
startIndex: parseInt(param.startIndex),
quantity: parseInt(param.quantity)
};
let returnData = {
code: "",
advertise: ""
};
// console.log("selectParams == ", selectParams);
// console.log(" loginUserInfo == ", loginUserInfo);
pool.getConnection( (err, connection) => {
if (err) {
throw err;
} else {
connection.query(
selectAdvertiseSql,
[selectParams.startIndex, selectParams.quantity],
function (err, rows) {
//console.log(result);
if (err) {
res.json({
code: "0000",
data: ""
});
} else {
// console.log("loginUserInfo ==", loginUserInfo);
returnData["code"] = "0001";
returnData["advertise"] = rows;
// res.json(returnData);
connection.query(
selectArticleSql,
[selectParams.startIndex, selectParams.quantity],
function (err, rows) {
returnData["article"] = rows;
// res.json(returnData);
connection.query(
selectTenantSql,
[selectParams.startIndex, selectParams.quantity],
function (err, rows) {
returnData["tenant"] = rows;
res.json(returnData);
}
)
}
)
}
connection.release();
}
)
}
})
});