2011-07-09 76 views
3

我的系统上的ulimit是1024.我遇到过很多关于同样错误但没有具体答案的帖子。我也想知道这是否是node/node-mysql问题,而不是用我的代码。我会后我的代码在这里的一个片段:Node.js node-mysql错误:EMFILE,打开的文件太多

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是我写这确实数据库连接

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; 
} 

我做得不对这里的node.js或者是它的驱动器的模块/node.js问题?谢谢您的帮助!

回答

2

我认为你应该更多地使用闭包。直接引用fn2将会使用命名对象,这将防止它超出范围。通过闭包引用相同的代码将导致一层匿名性,这将允许引用正常退出范围并进行垃圾收集。

相关问题