3

Hi I am new to nodejs and trying the connect the Mysql.
I am really troubled with the error Cannot enqueue Query after fatal error.
a) I am not using connection pooling so as not to complicate for start.
b) I had written connection.connect() at the start of code but removed it as I read in a link that it is not required. (refer below link)
Cannot enqueue Handshake after invoking quit

Below is the format for all my queries.

function matchFirstName(person_name){
              var query = "select firstName,lastname, username,securityRole from users where firstName='"+person_name+"'" ;
              connection.query(query, 
                function (err, rows) {
                    if (err || rows.length === 0) {
                        if(err){
                            console.log("error " + err);                
                            connection.end();
                        }
                    }
                    else{  
                        // some code or call to a different function which too might call a query in the same manner
                    }
            });
        }


Someone please suggest a permanent solution to this.
I have tried removing connection.end() from my code as well but that too didnt help. This error pops up suddenly.

Update: I think I found the reason. First some query that I use (a query which normally works) gives a ETIMEDOUT issue and then every following query fails. I increased the connectTimeout from 60000 to 120000 ms and added the following code.

connection.on('error', function (err){
     if(err.code === 'ETIMEDOUT' ){
         connection.connect();
     }
});

Will this solve my problem?

Community
  • 1
  • 1
Mitesh Shah
  • 61
  • 1
  • 4
  • seems like that you have an previous error , is your query is the first qiery in your program ? and in which line exactly you got that error ? – hassan Mar 05 '17 at 08:04
  • @HassanAhmed - No this is not a first query. Also I edited my finding above I am getting a ETIMEDOUT error and then all my following queries fail. – Mitesh Shah Mar 05 '17 at 08:16
  • so , i think that the error may be normal behavior , this maybe because an uncaught exception in a past failed query . – hassan Mar 05 '17 at 08:19
  • @HassanAhmed - So is my solution to add the error handler correct then? I have added this just below create.connection . Or should this be added to every query ? – Mitesh Shah Mar 05 '17 at 08:24
  • or with better way , fix the query which cause the failing . – hassan Mar 05 '17 at 08:31

0 Answers0