使用我的节点应用程序几次后,我总是遇到太多MySQL查询的问题。目前我使用的是MySQL连接池,它已经提高了稳定性,但不是最终的解决方案。
有没有更好的方法连接到MySQL数据库并在查询后直接关闭连接?
一般" MySQL连接":
const mysql = require('mysql');
const config = require('config');
const dbConfig = config.get('Azure.dbConfig');
console.log(`used mysql information: ${dbConfig}`);
const con = mysql.createPool(dbConfig
);
con.on('enqueue', () => {
console.log('connection enqued')
});
con.on('connection', function (connection) {
connection.query('SET SESSION auto_increment_increment=1')
});
con.on('release', function (connection) {
console.log('Connection %d released', connection.threadId);
});
module.exports = con;

MySQL查询示例:
const con = require('../db.js');
const bodyParser = require('body-parser');
module.exports = function (app) {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/dbCompareName', function (req, res) {
const ingredients = [];
con.query('SELECT wm_wine.name, wm_wine.avocandoIngredient, wm_wine.jahrgang, wm_wine.sorte, wm_cart.anzahl, wm_wine.typ from wm_wine INNER JOIN wm_cart on wm_cart.id = wm_wine.id', function (err, result, info) {
if (err) {
console.log('while querying the mysql database, an error occurred');
console.log(err);
if (err.fatal === true) {
con.connect(err => {
if (err) throw err;
logger.info('reconnected to mysql database');
});
};
}
else {
const transfer = result;
};
};