我在PCF上連接到mariadb實例上有一個nodejs應用程序。實例wait_timeout
爲600,因此在閒置後10分鐘後將斷開連接。PCJ上的Nodejs Mariadb超時未觸發mariadb事件
在我的本地機器上運行此應用程序並連接到MariaDB的本地實例上泊塢窗(一旦超時命中或當我殺死連接),我會得到一個MySQL Server has gone away
然後我的應用程序將重新連接,並給我的數據庫的結果。它不會在本地崩潰。
當PCF運行這個MariaDB的錯誤會導致應用程序崩潰或者異常被捕獲就會死機併發送Headers already sent error
我認爲,一旦MariaDB的殺死連接不知何故在節點MariaDB的驅動程序不通知即使我在連接上有事件處理程序,連接也被終止。因此,一旦我的應用程序嘗試運行查詢,它會失敗,因爲事件沒有被觸發,並試圖在關閉的連接上運行它。
我的連接模塊
// Loading local .env vars
var route_helper = require("./route_helper");
route_helper.loadLocalEnvVars();
var Client = require('mariasql');
var moment = require('moment');
var connection = null;
try{
connection = new Client({
host: process.env.DB_MARIA_HOST,
user: process.env.DB_MARIA_USER,
password: process.env.DB_MARIA_PASS,
db: process.env.DB_MARIA_SCHEMA,
port: parseInt(process.env.DB_MARIA_PORT),
connTimeout: 20,
pingInterval: 1,
compress: true
});
connection.connect(function(err){
if(!err) {
if(process.env.DEBUG == "true"){
console.log("Database is connected");
}
} else {
console.log("Error connecting database " + err.message);
console.log(err);
}
});
connection.on('ready',function(){
if(process.env.DEBUG == "true"){
console.log("Connection ready " + moment().format('MMMM Do YYYY, h:mm:ss a'))
}
});
connection.on('close',function(){
if(process.env.DEBUG == "true"){
console.log("Connection closed " + moment().format('MMMM Do YYYY, h:mm:ss a'))
}
});
connection.on('error',function(err){
if(process.env.DEBUG == "true"){
console.log("Connection timeout... Reconnecting")
console.log(err);
}
if(err.code == 2006){
this.connect();
}
//this.connect();
});
}catch(err){
console.log("Maria_db error")
console.log(err)
throw err;
}
module.exports = connection;
加載服務器路由器
var routes = require("./routes/application.js").appRouter(server);
var server = server.listen(process.env.PORT, function() {
console.log("Listening on port %s...", server.address().port);
});
我的路線
app.get("/applications", function (req, res) {
try{
var applications;
var dbConn = require("../util/db.js");
dbConn.query('SELECT some stuff’, function (error, results) {
if(!error) {
applications = {
"apps": results
}
return res.send(applications);
} else {
return res.send({"status": "error", "message": error.code});
}
});
}catch(err) {
console.log(err)
}
});
MySQL已經走了錯誤
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client.<anonymous> (/home/vcap/app/util/dbHealth.js:14:9)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at emitOne (events.js:101:20)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client.emit (events.js:191:7)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client._onerror (/home/vcap/app/node_modules/mariasql/lib/Client.js:395:10)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at process._tickCallback (internal/process/next_tick.js:104:9)
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! Exit status 1
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! Failed at the [email protected] start script 'node server.js'.
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! not with npm itself.
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! You can get information on how to open an issue for this project with:
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! npm bugs myapplication
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! Or if that isn't available, you can get their info via:
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! npm owner ls myapplication
2017-12-20T09:03:55.33-0500 [APP/PROC/WEB/0] ERR npm ERR! Please include the following file with any support request:
2017-12-20T09:03:55.33-0500 [APP/PROC/WEB/0] ERR npm ERR! /home/vcap/app/.npm/_logs/2017-12-20T14_03_55_316Z-debug.log
標題已發送錯誤。他們似乎也沒有秩序,這很奇怪。在此測試期間,我向3個不同的服務發送3個http請求並獲取此錯誤。
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at _combinedTickCallback (internal/process/next_tick.js:131:7)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at process._tickCallback (internal/process/next_tick.js:180:9) code: 2006 }
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR _http_outgoing.js:494
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at validateHeader (_http_outgoing.js:494:11)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.send (/home/vcap/app/node_modules/express/lib/response.js:158:21)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at Object.cb (/home/vcap/app/routes/application.js:474:29)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at _combinedTickCallback (internal/process/next_tick.js:131:7)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at process._tickCallback (internal/process/next_tick.js:180:9) code: 2006 }
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR throw new Error('Can\'t set headers after they are sent.');
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR Error: Can't set headers after they are sent.
2017-12-27T11:07:46.41-0500 [APP/PROC/WEB/0] OUT Connection timeout... Reconnecting
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at _combinedTickCallback (internal/process/next_tick.js:131:7)
2017-12-27T11:07:46.41-0500 [APP/PROC/WEB/0] OUT Route: /applications
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.setHeader (_http_outgoing.js:501:3)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.header (/home/vcap/app/node_modules/express/lib/response.js:767:10)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at process._tickCallback (internal/process/next_tick.js:180:9) code: 2006 }
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR ^
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.send (/home/vcap/app/node_modules/express/lib/response.js:170:12)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.json (/home/vcap/app/node_modules/express/lib/response.js:267:15)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at cleanupReqs (/home/vcap/app/node_modules/mariasql/lib/Client.js:744:11)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at Client._onclose (/home/vcap/app/node_modules/mariasql/lib/Client.js:574:5)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at Client._onerror (/home/vcap/app/node_modules/mariasql/lib/Client.js:396:10)
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! Failed at the [email protected] start script.
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR npm ERR! code ELIFECYCLE
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR!
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! [email protected] start: `node server.js`
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! Exit status 1
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! errno 1
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! A complete log of this run can be found in:
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! /home/vcap/app/.npm/_logs/2017-12-27T16_07_46_432Z-debug.log
編輯:用招搖
難道是'SELECT some stuff'需要10分鐘以上嗎?請隔離聲明並確定這是真是假。如果速度很慢,那麼讓我們來關注它,而不是其他的東西(PDF,Header,ERRs等) –
select只選擇1行或最大1000.查詢運行很快,但我無法解決這個問題。看到我的「答案」。我只需要切換驅動程序。 – tiggles