简体   繁体   English

Felixge设计中带有MySQL的Node.js和奇怪的行为

[英]Node.js with mysql from felixge design and strange behaviour

Hello I noticed some strange behaviour in node.js with felix geisendörfers awesome mysql module. 您好,我注意到使用felixgeisendörfers很棒的mysql模块在node.js中出现了一些奇怪的行为。

I have the following structure in my express app. 我的Express应用程序具有以下结构。

  • app.js (main) app.js(主)
  • routesA.js routesA.js
  • routesB.js routesB.js
  • routesC.js routesC.js

The routes use the Router function of express. 路由使用Express的路由器功能。 Every routing file (AC) has to access the mysql server. 每个路由文件(AC)必须访问mysql服务器。 But I was to lazy to write a connection and the connection options in every routing script file. 但是我懒于在每个路由脚本文件中编写一个连接和连接选项。 So I made another file called DBServer.js It is as follows (inspired by some hints from someone here in stackoverflow): 因此,我制作了另一个名为DBServer.js的文件,如下所示(灵感来自stackoverflow中某人的一些提示):

var mysql = require('mysql');

exports.connect = function (){      
  var db_config = {
    host: '127.0.0.1', 
    user: 'my username', 
    password: '********', 
    database: 'my database'
  };

  var connection;

  function autoConnect() {

    connection = mysql.createConnection(db_config);

    connection.connect(function(err) {
      if(err) {
        console.log('DBServer Error: cannot connect to db. Reconnect attempt in 2 seconds...\nError: ', err);
        setTimeout(autoConnect, 2000); 
      }
      else{
        console.log('DBServer connected successfully...');
      }
    });

    connection.on('error', function(err) {
      if(err.code === 'PROTOCOL_CONNECTION_LOST') {
        console.log('DBServer Error: lost connection. Reconnect attempt in 2 seconds...\nError: ', err);
        autoConnect();
      } 
      else {
        console.log('DBServer Error: minor error\nError: ', err);
      }
    });

  }

  autoConnect();

  return connection
}

In every routing file I require this DBServer file via: 在每个路由文件中,我都需要通过以下方式获取此DBServer文件:

var db = require('./lib/DBServer').connect();

When I start my app, the console logs 3 times 当我启动我的应用程序时,控制台记录了3次

DBServer connected successfully...
DBServer connected successfully...
DBServer connected successfully...

... as intended. ... 如预期。

Everything works perfect. 一切正常。 I run the App with forever and every time the script losses connection to the db-server (what happens from time to time) it reconnects again... as intended. 我永远运行该应用程序,并且每次脚本断开与db服务器的连接(有时会发生情况)时,它都会再次重新连接...如预期的那样。

... BUT! ...但是! Except of one script. 除了一个脚本。 Script routesA.js stops working when it comes to a mysql query. 涉及mysql查询时,脚本routeA.js停止工作。 The script freezes but does not quit. 脚本冻结,但不退出。 I have to stop and restart it again. 我必须停止并重新启动它。 There is no difference between the invocation of DBServer.js between routesA, routesB or routesC. 在routeA,routeB或routeC之间对DBServer.js的调用之间没有区别。 And it works pretty good... But it seems that if routesA looses connection it does not reconnect again... routesB and routesC still works fine. 它的工作原理还不错……但是似乎如果routeA失去了连接,它就不会再重新连接了……routeB和routeC仍然可以正常工作。

So I changed the way how script routesA.js connects to the database. 因此,我更改了脚本routeA.js连接到数据库的方式。 I connect now in script routesA.js not via the DBServer.js and require but the manual way 我现在不是通过DBServer.js通过脚本routeA.js连接,而是需要手动方式

var mysql      = require('mysql');
var db = mysql.createConnection({
  host: '127.0.0.1', 
  user: 'my username', 
  password: '******', 
  database: 'ma database'
});

db.connect(); 

Now it works... and runs for days without problems. 现在它可以工作了……而且可以连续运行几天而不会出现问题。 But the reason why this works is, because now I don't have an error handling in script routesA.js.. so forever detects a script exit and restarts... and everything works again. 但这有效的原因是,因为现在我在脚本routeA.js中没有错误处理,所以永远检测到脚本退出并重新启动...,然后一切又恢复了。 But I don't want that way. 但是我不想那样。 I want a proper error handling like in DBServer.js. 我想要像在DBServer.js中那样进行适当的错误处理。 As said this works for script B and C, but not script A... 如前所述,它适用于脚本B和C,但不适用于脚本A ...

I know that it is strange and difficult to say, what might be the problem. 我知道这可能是问题,这很奇怪且很难说。 But maybe someone has had some similar problem. 但是也许有人遇到过类似的问题。

Another question here is: How do you handle the database connections with multiple script files. 这里的另一个问题是:如何处理具有多个脚本文件的数据库连接。 I there a way to share one mysql connection for all script files you have in an app? 我有一种方法可以共享一个应用程序中所有脚本文件的mysql连接吗?

kind regards martin 亲切的问候马丁

The reason probably has to do with the fact that you're returning the initial connection object on require() , but if you get disconnected, you reassign the connection variable which the external scripts do not have a reference to (they still only have a reference to the old/original connection object). 原因可能与您在require()上返回初始连接对象有关,但如果断开连接,则重新分配外部脚本没有引用的连接变量(它们仍然只有一个引用旧/原始连接对象)。

I should also note that if you're using the mysql2 module (compatible with mysql except much faster), there is a connection.ping() method that you can use to periodically ping the server to help keep the connection alive. 我还应该注意,如果您使用的是mysql2模块(与mysql兼容,但速度要快得多),则有一个connection.ping()方法可用于定期对服务器执行ping操作,以帮助保持连接状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM