'Error: Cannot enqueue Query after fatal error in mysql node

I am using felixge/node-mysql. Also I am using express-myconnection which prevents mysql timeout and in turn prevents killing of node server. What I am doing is logging the activities in mysql. The scenario is I have a file upload functionality once the file is uploaded I am performing different operations on the file. During every stage of processing I am logging those activities in database. This works fine if the file is small. If the file is large say 100 MB it takes some time to load so in the mean time the mysql server reconnects and creates a new connection but the logging code still uses the old reference. Error: Cannot enqueue Query after fatal error. So, my question is is there a way that i can use the new connection reference instead of the old one. There is a single function in which all the different phases of activities regarding file takes place. Any help is greatly appreciated. thanks

Hi @paul, if you have seen the gist link you can see that I have the upload.on('begin', function (fileInfo, reqs, resp) { } where I have logged the activity that file upload process has begin. Once the file is uploaded upload.on('end', function (fileInfo,request,response) { } is triggered. I am also logging some activity here. As I said in my question, if the file is big the upload takes time. In the mean time a new MySql connection is created but the insert query in 'end' event still refers to the old myconnection. So, I wanted to know how can I use the new mysql connection reference in this scenario? I hope this has explained the scenario better.



Solution 1:[1]

Actually, I decided to google your error for you, and after reading this thread: https://github.com/felixge/node-mysql/issues/832 I realized that you're not releasing the connection after the first query completes, and so the pool never tries to issue you a new one. You were correct that the stale connection might be the problem. here's how you fix that if it is:

upload.on('begin', function (fileInfo, reqs, resp) { 
    var fileType = resp.req.fields.file_type;
    var originalFileName = fileInfo.name;
     var renamedFilename = file.fileRename(fileInfo,fileType);
    /*renaming the file */
    fileInfo.name = renamedFilename;

    /*start: log the details in database;*/
    var utcMoment = conf.moment.utc();
    var UtcSCPDateTime = new Date( utcMoment.format() );
    var activityData = {
        activity_type     : conf.LIST_UPLOAD_BEGIN,
        username          : test ,
        message           : 'test has started the upload process for the file',
        activity_datetime : UtcSCPDateTime 
    };
    reqs.params.activityData = activityData;
    req.getConnection(function(err,connection) {
    var dbData = req.params.activityData;
    var activity_type = dbData.activity_type;
    console.dir("[ Connection ID:- ] "+connection.threadId+' ] [ Activity type:- ] '+activity_type);
    var insertQuery = connection.query("INSERT INTO tblListmanagerActivityLog SET ? ",dbData, function(err, result) {
        if (err) {
            console.log("Error inserting while performing insert for activity "+activity_type+" : %s ",err );
        } else {
            console.log('Insert successfull');
        }
        /// Here is the change:
        connection.release();
    });

});
    /*end: log the details in database;*/
});

Solution 2:[2]

I fixed mine. I always suspect that when errors occur along with my queries it has something to do with the MySQL80 Service being stopped in the background. In case other solutions failed. Try going to the task manager, head to the services, find MySQL80 and check if it is stopped, when it is, click start or set it as automatic so that it will start at the moment desktop is running.

Solution 3:[3]

For pool connection to work we need to comment out https://github.com/pwalczyszyn/express-myconnection/blob/master/lib/express-myconnection.js#L84 this line. Hope it helps anyone facing the same issue.

Also we can use single connection option.

Solution 4:[4]

I had the same issue. Came across one solution -

Re-Install MySQl and while doing so, in the configuration step, select "legacy encryption" option instead and finish the installation.

Hope this helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul
Solution 2 Dimes
Solution 3 Rahul Singh
Solution 4 Arunabh Arjun