首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nodejs Mariadb on PCF超时不触发mariadb事件

Nodejs Mariadb on PCF超时不触发mariadb事件
EN

Stack Overflow用户
提问于 2017-12-27 23:13:23
回答 1查看 229关注 0票数 0

我在PCF上有一个nodejs应用程序,它连接到一个mariadb实例。实例wait_timeout为600,处于非活动状态后将在10分钟后断开连接。

在我的本地机器上运行此应用程序并连接到docker上的mariadb的本地实例(一旦超时或当我终止连接时),我将获得一个MySQL Server has gone away,然后我的应用程序将重新连接并给出我的数据库结果。它不会在本地崩溃。

在PCF上运行时,mariadb错误将使应用程序崩溃,或者如果捕获到异常,它将崩溃并发送Headers already sent error

我相信,一旦MariaDB以某种方式终止了连接,即使我在连接上有事件处理程序,节点中的mariadb驱动程序也不会收到连接终止的通知。因此,一旦我的应用程序尝试运行查询,它就会失败,因为事件没有被触发,并且它试图在关闭的连接上运行它。

我的连接模块

代码语言:javascript
复制
// 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;

加载服务器路由器

代码语言:javascript
复制
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);
});

我的路线

代码语言:javascript
复制
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已离开错误

代码语言:javascript
复制
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 myapplication@1.0.0 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请求,并得到了这个错误。

代码语言:javascript
复制
   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 myapplication@1.0.0 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! myapplication@1.0.0 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

编辑:此应用程序结构是由swagger生成的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-02 23:06:04

我从只有mariasql npm驱动切换到knex驱动。即使knex dependancies有mariasql: '^0.2.3',它开箱即用,没有任何问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47994478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档