开发者

Node.js node-mysql Error: EMFILE, Too many open files

开发者 https://www.devze.com 2023-03-18 21:12 出处:网络
The ulimit on my system is 1024. I have come across many posts regarding the same error but no concrete answer. I also want to know if this is a node/node-mysql issue and not to do with my code. I wil

The ulimit on my system is 1024. I have come across many posts regarding the same error but no concrete answer. I also want to know if this is a node/node-mysql issue and not to do with my code. I will post a snippet of my code here:

exports.fn1  = function(req,res,host,user,password,database){
        var client = connectDB.connectDatabase(host,user,password,database);
                fn2();

    function fn2() {
        client.query(
            'select statement', args,
            function selectCb(error, result, fields) {
                if (error) {
                    console.log('ERROR');
                    client.end();
                    return;
                }
              
                if(not timeout) {
                    setTimeout(function(){
                                    
                                    fn2();
                               },5000);
                }
                
                else {
                    res.writeHead(200, {'Content-Type': 'text/plain'});
                    res.write(somedata);
                    res.end(); 
                    client.end();   
                }


        });
        }
    }
};

connectDB is a module I wrote which does the db connections

var Client = require('mysql').Client; 

exports.connectDatabase = function(host,user,password,database) {
    var client = new Client(); //Database connection object
    //Credentials for DB connection
    client.host = host; 
    client.user = user;
    client.password = password;
    client.database = database;

    client.connect(function(error, results) {
        if(error) {
          console.log('Connection Error:');
          return;
        }
    });
    return client;
}

Am I doi开发者_如何学JAVAng something wrong with node.js here or is it a driver/node.js problem? Thanks for the help!


I think you should be using closures more. References directly to fn2 are going to be using the named object, which will keep it from going out of scope. References to the same code through a closure will cause a layer of anonymity, which will allow the references to exit scope gracefully and get garbage collected.


I faced this problem too, it's gotta do with async and need to solve it using closure.

node.js asychronous issue?

0

精彩评论

暂无评论...
验证码 换一张
取 消