0

I have already read the post Node Mysql Cannot Enqueue a query after calling quit, the conn.end() is in my query block.

My issue is that conn.end in my dashboard.js does not work and breaks the app with the following error.

I need to use it because MySQL connections are flooding the system till Database stop accepting more of them since they all stay open each time that are used.

For this post I will use only one (dashboard.js) of the several routes of my NodeJS application.

`Event.js` is not a part of my working files, probably is a file from `./node_modules`

|events.js:183 
      throw er; // Unhandled 'error' event
      ^

Error: Cannot enqueue Quit after invoking quit.
    at Protocol._validateEnqueue (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:204:16)
    at Protocol._enqueue (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:139:13)
    at Protocol.quit (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:92:23)
    at Connection.end (E:\NodeJS\Project-Change\node_modules\mysql\lib\Connection.js:249:18)
    at ServerResponse.res.end (E:\NodeJS\Project-Change\node_modules\express-myconnection\lib\express-myconnection.js:114:54)
    at ServerResponse.send (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:191:8)
    at fn (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:896:10)
    at View.exports.renderFile [as engine] (E:\NodeJS\Project-Change\node_modules\ejs\lib\ejs.js:323:3)
    at View.render (E:\NodeJS\Project-Change\node_modules\express\lib\view.js:76:8)
    at Function.app.render (E:\NodeJS\Project-Change\node_modules\express\lib\application.js:527:10)
    at ServerResponse.res.render (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:900:7)
    at Query._callback (E:\NodeJS\Project-Change\routes\dashboard.js:39:17)
    at Query.Sequence.end (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
    at Query._handleFinalResultPacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
    at Query.EofPacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
    at Protocol._parsePacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:279:23)

app.js (only relative lines)

var express  = require('express'),
    path     = require('path'),
    bodyParser = require('body-parser'),
    app = express(),
    expressValidator = require('express-validator'),
    session = require('express-session'),
    passport = require('passport'),
    flash = require('connect-flash'),
    passportConfig = require('./config/passport'),
    dbConfig = require('./config/db');

// skipping code about static files, bodyparser, expressValidator, session, passport

passportConfig(passport)

/*MySQL connection*/
var connection  = require('express-myconnection'),
    mysql = require('mysql');

app.use(
    connection(mysql, dbConfig,'request')
);

var dashboard = require('./routes/dashboard.js'); // in this route I apply conn.end()
var router = require('./routes/rates.js');

// skipping app.post/app.get code for /login & /logout & isLoggedIn middleware

app.use('/', router);
app.use('/', dashboard); // issue on that

app.get('/',function(req,res){
    res.render('./dashboard.ejs'); //issue on that
});

module.exports = app;

routes/dashboard.js (route)

var express = require('express');
var router = express.Router();

var dashboard = express.Router();

dashboard.use(function(req, res, next) {
    console.log(req.method, req.url);
    next();
});

var dashboard = router.route('/');


//show the CRUD interface | GET
dashboard.get(function(req,res,next){

    req.getConnection(function(err,conn){

        if (err) return next("Cannot Connect");

        var query = conn.query('SELECT SUM(total_price) AS transactions_total FROM transactions WHERE date_created = CURDATE(); SELECT SUM(total_profit) AS total_profit FROM transactions',  function(err,rows){


            if(err){
                console.log(err);
                return next("MySQL error, check your query");
            }

            var ab = {data:rows[0]};
            var total_profit = {data:rows[1]};

            res.render('dashboard',{ab, total_profit});

            conn.end(); // causes the described error

         });

            // conn.end(); even tried here

    });

});

dashboard.all(function(req,res,next){
    console.log("route for dashboard executed");
    console.log(req.params);
    next();
});


module.exports = router;
console.log('dashboard.js loaded!');

config/db.js

module.exports = {
          host     : 'localhost',
          user     : 'root',
          password : '',
          database : 'mydb',
          multipleStatements: true, 
          debug    : false
}

config/passport.js

External presentation in case is needed here

Cadmos
  • 277
  • 4
  • 21
  • 1
    Can you please check if connection is open before you run your “try/catch” block with connection.end.. – boateng May 18 '18 at 15:11
  • @numbtongue I did by putting in `dashboard.js` , right after `req.getConnection(function(err,conn){ console.log(conn.state,conn.threadId ) ... ` - Results: `authenticated 2856` - However if it matters, `dashboard.js` route is the initial page that is rendered after the user logs in – Cadmos May 18 '18 at 15:56
  • @numbtongue Full code concerning your suggestion on this [link](https://pastebin.com/9fPKpxnv) – Cadmos May 18 '18 at 16:01
  • Thanks.. instead of calling connection.end after process ends you could be try calling it just before.. https://stackoverflow.com/a/31454984/3254405 – boateng May 18 '18 at 20:10
  • @numbtongue Thank you. I also tried it with no result, actually with neither bad or good result, like it was ignored. I also tried to put `conn.connect()` before the query, however in this way the error changes to - `Cannot enqueue Handshake after already enqueuing a Handshake` – Cadmos May 18 '18 at 21:28
  • I’ve noticed on the line starting var query = .. you specified 2 separate queries, is this deliberate.. Could it be the cause.. – boateng May 18 '18 at 21:42
  • @numbtongue yes, I work with multiple statements on as you can check on db settings. In any case the issue exists also in routes with one query statement on which I also have tried to apply `conn.end()` – Cadmos May 18 '18 at 21:49
  • Ok.. in this example it shows how to connect to MySQL on start of the app and no need for connection.end https://www.terlici.com/2015/08/13/mysql-node-express.html – boateng May 18 '18 at 22:01

0 Answers0