在非工作时间,DB进入理想状态,所以我再次重新连接数据库,如下所示:
执行数据库查询时出现以下错误:
{ "状态":500, "错误":{ "代码":" PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR", "致命":假 } }
var mysql = require('mysql');
var settings = require('./config.js');
var connection;
function handleDisconnect() {
connection = mysql.createConnection({
host: settings.dbhost,
user: settings.user,
password: settings.password,
database: settings.database
});
connection.connect(function (err) {
if (err) {
console.log('Error when connecting to db:', err, settings.database);
setTimeout(handleDisconnect, 2000);
}
else {
console.log("Database is connected ...", settings.database);
}
});
connection.on('error', function (err) {
console.log('db error', err, settings.database);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
connection
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
module.exports = connection;
答案 0 :(得分:0)
一种可能但不完善的解决方案是使用sql.ConnectionPool()