【发布时间】:2021-04-23 22:19:23
【问题描述】:
仍在玩我的第一个 API NodeJS 应用程序。
我当前的 MYSQL 配置是:
const mysql = require('mysql');
var pool = mysql.createPool({
host:'localhost',
user: 'abc',
password: 'abc',
database : 'abc',
multipleStatements: true,
connectionLimit: 10
});
module.exports ={
connection : pool
}
我的“订单列表”部分是获取未完成订单列表的一种方式。 此页面工作正常,但在单击几个按钮后会显示列表,程序会挂起,直到我重新启动 API。
代码:
const express = require('express');
const router = express.Router();
const verify = require('../../verifyToken');
var config = require('../../databaseConfig');
var connection= config.connection;
router.get('/getMainPageCount/',verify,(req,res) => {
const chemistID = req.user._id;
connection.getConnection(function(error,tmpConn){
if(error){
console.log("error in connecting to MYSQL " + error);
res.sendStatus(500);
}else{
var sqlString = "SET @chemistID = ?; CALL getMainPageCount(@chemistID);";
tmpConn.query(sqlString,[chemistID], async (err,rows,fields)=>{
try{
const ListData = rows[1];
//console.log(rows);
//console.log(chemistID);
if (rows && Array.isArray(rows) && rows.length) {
return res.status(200).json({
Result: true,
orders: ListData
})
}
else
{
return res.status(500).json({
Result: false
})
}
} catch (e) {
// this catches any exception in this scope or await rejection
console.log(e);
res.status(500).json({ Result: e });
}
})
}
})
});
router.get('/getOpenRequests/',verify,(req,res) => {
const chemistID = req.user._id;
var sqlString = "SET @chemistID = ?; CALL getOpenRequests(@chemistID);";
connection.getConnection(function(error,tmpConn){
if(error){
console.log("error in connecting to MYSQL " + error);
res.sendStatus(500);
}else{
try{
tmpConn.query(sqlString,[chemistID], async (err,rows,fields)=>{
const ListData = rows[1];
const foundRows = rows[2][0].rowCount;
////console.log(rows[1][0].FirstName);
//console.log(chemistID);
if (rows && Array.isArray(rows) && rows.length) {
return res.status(200).json({
Result: true,
orders: ListData,
rowCount: foundRows})
}
else
{
return res.status(500).json({
Result: false
})
}
})
} catch (e) {
// this catches any exception in this scope or await rejection
console.log(e);
res.status(500).json({ Result: e });
}
}
})
});
router.get('/getOpenOrders/',verify,(req,res) => {
const chemistID = req.user._id;
var sqlString = "SET @chemistID = ?; CALL getOpenOrders(@chemistID);";
connection.getConnection(function(error,tmpConn){
if(error){
console.log("error in connecting to MYSQL " + error);
res.sendStatus(500);
}else{
try{
tmpConn.query(sqlString,[chemistID], async (err,rows,fields)=>{
const ListData = rows[1];
const foundRows = rows[2][0].rowCount;
//console.log(rows[1][0].FirstName);
if (rows && Array.isArray(rows) && rows.length) {
return res.status(200).json({
Result: true,
orders: ListData,
rowCount: foundRows})
}
else
{
return res.status(500).json({
Result: false
})
}
})
} catch (e) {
// this catches any exception in this scope or await rejection
console.log(e);
res.status(500).json({ Result: e });
}
}
})
});
router.get('/getOpenFinalised/',verify,(req,res) => {
const chemistID = req.user._id;
var sqlString = "SET @chemistID = ?; CALL getOpenFinalised(@chemistID);";
connection.getConnection(function(error,tmpConn){
if(error){
console.log("error in connecting to MYSQL " + error);
res.sendStatus(500);
}else{
try{
tmpConn.query(sqlString,[chemistID], async (err,rows,fields)=>{
const ListData = rows[1];
const foundRows = rows[2][0].rowCount;
//console.log(rows[1][0].FirstName);
//console.log(chemistID);
if (rows && Array.isArray(rows) && rows.length) {
return res.status(200).json({
Result: true,
orders: ListData,
rowCount: foundRows
})
}
else
{
return res.status(500).json({
Result: false
})
}
})
} catch (e) {
// this catches any exception in this scope or await rejection
console.log(e);
res.status(500).json({ Result: e });
}
}
})
});
router.get('/getOpenOverdue/',verify,(req,res) => {
const chemistID = req.user._id;
var sqlString = "SET @chemistID = ?; CALL getOpenOverdue(@chemistID);";
connection.getConnection(function(error,tmpConn){
if(error){
console.log("error in connecting to MYSQL " + error);
res.sendStatus(500);
}else{
try{
tmpConn.query(sqlString,[chemistID], async (err,rows,fields)=>{
const ListData = rows[1];
const foundRows = rows[2][0].rowCount;
//console.log(rows[1][0].FirstName);
//console.log(chemistID);
if (rows && Array.isArray(rows) && rows.length) {
return res.status(200).json({
Result: true,
orders: ListData,
rowCount: foundRows})
}
else
{
return res.status(500).json({
Result: false
})
}
})
} catch (e) {
// this catches any exception in this scope or await rejection
console.log(e);
res.status(500).json({ Result: e });
}
}
})
});
直到我开始使用此池连接和错误处理后才出现此问题。
现在我对 NodeJS(和一般编码)非常陌生,但如果我在终端上运行它:
lsof -i tcp:3000
然后我可以看到该端口上运行的所有内容。 当我使用 ot 这样做(预连接池)时,我只会得到节点:我的 Listen 的 PID。
现在,在运行任何 API 端点大约 10 秒后,我得到了这个:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 18993 myserver 18u IPv6 198273222 0t0 TCP *:hbci (LISTEN)
node 18993 myserver 19u IPv6 198273223 0t0 TCP vps.myserver.com.au:hbci->myIPAddress:59264 (ESTABLISHED)
现在我假设这是仍然存在的连接...所以连接没有被关闭? 这可能是问题吗? 我只是不明白我的错误处理有什么问题,我得到这个挂起而不是崩溃或其他什么
我不得不更改为池连接,因为它在这里和那里失去了连接,并且在阅读了它之后显然池是最好的方法,但我一定错过了什么? 我需要释放连接吗?
【问题讨论】:
标签: node.js express database-connection