node.js mysql-simple-pool在12个小时后停止响应

时间:2013-12-23 18:15:42

标签: mysql node.js

起初我认为它与垃圾收集有关,但是进一步研究问题,似乎mysql不再响应我的任何查询。

我的每个模型都有一个require('。/ db.js');在他们的文件的顶部,看起来像:

var Pool = require('mysql-simple-pool');

var mysql_pool = new Pool(50, {
    host: process.env.IP || '127.0.0.1', //currently the db is on the server
    user: 'USERNAME',
    password: 'PASSWORD',
    database: 'DATABASE'
});

module.exports = mysql_pool;

效果很好,我可以做以下事情:

db.query('select * from user where username = ? and password = ?', [username, hashed_password], function(err, rows) {
    if (err) { 
        console.log("error in loginUser", err);
        return callback(err, null); 
    } else {
        if (rows[0]) {
            var user = new User(rows[0]);
            return callback(err, user);
        } else {
            return callback(err, null);
        }
    }
});

除了每隔12个小时,它才会停止接听。当人们尝试登录时,它只是坐在那里,我最终得到一个超时错误。

如果用户已经登录,那么来自内存的所有内容都很棒,但我查询数据库的部分却无法响应。

更新: 我在代码中找到了一点,我只是想想这应该是什么。

connection.on('error', function(err) {
    // Check if the connection has been lost.
    if (err.fatal && err.code !== 'PROTOCOL_CONNECTION_LOST') {
        // Decrement the current number of _connections.
        pool._currentNumberOfConnections--;
    }
});

由于某种原因,当协议连接丢失时,它没有做任何事情。我想知道是否有一种方法可以在连接丢失或重新建立连接后重新建立连接?我假设问题是连接打嗝,因为我正在使用游泳池,他们基本上都会迷路或卡住,而他们只是坐在那里。

1 个答案:

答案 0 :(得分:0)

转离mysql-simple-pool

db.php现在看起来像:

var easy_mysql = require('easy-mysql');

var options = {
    host: process.env.IP || '127.0.0.1',
    user: 'USERNAME',
    password: 'PASSWORD',
    database: 'DATABASE',
    pool_size: 50
};

var pool = easy_mysql.connect_with_easy_pool(options);

//this is here so it fills in the place of mysql-simple-pool while i port stuff over
module.exports.query = function(sql, params, callback) {
    if ("function" == typeof params) {
        callback = params;
        params = [];
    }
    pool.get_all(sql, params, callback);
};

module.exports.get_all = pool.get_all; //these are here so i can slowly port stuff over
module.exports.get_one = pool.get_one; 
module.exports.execute = pool.execute;
相关问题