Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot enqueue Query after fatal error. #1694

Closed
PoojaKhandhadia opened this issue Apr 14, 2017 · 3 comments
Closed

Error: Cannot enqueue Query after fatal error. #1694

PoojaKhandhadia opened this issue Apr 14, 2017 · 3 comments
Labels

Comments

@PoojaKhandhadia
Copy link

PoojaKhandhadia commented Apr 14, 2017

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.
This is my code in server.js where I am trying to connect with database.

var config = {
    user : 'root',
    database : 'stu_corner',
    host : '127.0.0.1',
    password :'xxxxxxxx',
};
var client = mysql.createConnection(config);
client.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    client.end();
  }else{
    console.log('Connection established');
}
});

This is also a code in server.js file where I am trying to log in,but when i enter the username and password and click for to log in the error status is 500 that means server error and in console it shows Error: Cannot enqueue Query after fatal error.

app.post('/admin_login' , function(req,res){
  var admin_username = req.body.admin_username;
  var admin_password = req.body.admin_password;
  client.query('select * from admin where username = ?',[admin_username],function(err,result){
    if (err){
      res.status(500).send(err.toString());
    }else{
      console.log()
      if (result.length === 0){
        res.status(403).send('Username/Password is invalid\n');
      }else{
        if (admin_password === result[0].password){
          //req.session.auth = {stu_id : result[0].stu_id};
          var username = JSON.stringify(result[0].username);
          res.writeHead(200, {'Content-Type':'application/json'});
          res.write(username);
          res.end();
        }else{
          res.status(403).send('Username/Password is wrong\n');
        }
      }
    } 
  });
});

Can someone please help me to solve this issue.

@dougwilson
Copy link
Member

Hi @PoojaKhandhadia the error Cannot enqueue Query after fatal error. means you are trying to perform a query on a connection that has encountered a fatal error, and is now dead & unusable. You can identify fatal errors by checking if err.fatal is true (https://github.com/mysqljs/mysql#error-handling). A fatal error is something unrecoverable, like the TCP connection got disconnected, a protocol error, or similar.

If you are not using the pool, you'll need to implement fatal error handing in your own code. What this means is that you need to check every err object you get back from this module for err.fatal to be true, and if it is, you need to create a brand new connection to the MySQL server and connect again and then start using that new connection for future queries.

I hope this helps!

@rafipiccolo
Copy link

this seems to work for me : mysql connection restarts everytime a fatal error occurs.

var mysql = require('mysql');
var config = require('./config.js');

function startConnection() {
    console.error('CONNECTING');
    connection = mysql.createConnection(config.mysql);
    connection.connect(function(err) {
        if (err) {
            console.error('CONNECT FAILED', err.code);
            startConnection();
        }
        else
            console.error('CONNECTED');
    });
    connection.on('error', function(err) {
        if (err.fatal)
            startConnection();
    });
}

startConnection();


// testing a select every 3 seconds :
// to try the code you can stop mysql service => select will fail
// if you start mysql service => connection will restart correctly => select will succeed
setInterval(function() {
    connection.query('select 1', function(err, results) {
        if (err) console.log('SELECT', err.code);
        else console.log('SELECT', results);
    });
}, 3000);

@dougwilson
Copy link
Member

Closing since there hasn't been a follow up from @PoojaKhandhadia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants