0

I'm using node-mysql. Below codes taken from http://blog.josedacruz.com/2013/07/19/learning-node-js-using-mysql/

var http = require('http');
var mysql = require('mysql');

var connection = mysql.createConnection({
  host : 'localhost',
  port : 3306,
  database: 'aaa',
  user : 'root',
  password : 'z'
});

http.createServer(function(req, res){
    res.writeHeader(200);
    res.write('Connect to mySql\n');
    // Connect to mySql (if there is an erro, report it and terminate de request)
    connection.connect(function(err){
        if(err != null) {
            res.end('Error connecting to mysql:' + err+'\n');
        }
    });

    connection.query("SELECT value FROM variable WHERE name = 'site_name'", function(err, rows){
        if(err != null) {
            res.end("Query error:" + err);
        } else {
            // Shows the result on console window
            console.log(rows[0]);
            res.end("Success!");
        }
        // Close connection
        connection.end();
    });
}).listen(80);

First thing first, I feel strange the connection.end not same level with connection.connect

I've tried change the connection to be same level and also put on different place, but still no luck

Below is the error in terminal when HTTP server stops after first run (note: MyTube is the result of query)

{ value: 'MyTube' }
 
Error: Cannot enqueue Quit after invoking quit.
    at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:131:16)
    at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
    at Protocol.quit (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:61:36)
    at Connection.end (/home/mydir/node/node_modules/mysql/lib/Connection.js:155:18)
    at Query._callback (/home/mydir/node/app.js:31:20)
    at Query.Sequence.end (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:153:6)
    at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
    at Connection.query (/home/mydir/node/node_modules/mysql/lib/Connection.js:140:25)
    at Server.<anonymous> (/home/mydir/node/app.js:22:16)
    --------------------
    at Quit.Sequence (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
    at new Quit (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Quit.js:8:12)
    at Protocol.quit (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:61:45)
    at Connection.end (/home/mydir/node/node_modules/mysql/lib/Connection.js:155:18)
    at Query._callback (/home/mydir/node/app.js:31:20)
    at Query.Sequence.end (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:153:6)
    at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
    at Connection.query (/home/mydir/node/node_modules/mysql/lib/Connection.js:140:25)
    at Server.<anonymous> (/home/mydir/node/app.js:22:16)

2 Answers 2

2

The problem is that you're trying to reuse a connection created by mysql.createConnection after that connection has been closed by connection.end().

You need to move the call to mysql.createConnection to inside the callback to http.createServer, or (which I would recommend) use connection pooling instead.

Sign up to request clarification or add additional context in comments.

Comments

1

One way to quickly solve this is to remove your connection.connect and connection.end calls and rely on the implicit connection made by connection.query

http.createServer(function(req, res){
    res.writeHeader(200);
    res.write('Connect to mySql\n');     
    connection.query("SELECT value FROM variable WHERE name = 'site_name'", function(err, rows){
        if(err != null) {
            res.end("Query error:" + err);
        } else {
            // Shows the result on console window
            console.log(rows[0]);
            res.end("Success!");
        }
    });
}).listen(80);

1 Comment

node-mysql queues operations so they don't have to be performed in the usual async way of waiting until a previous operation has finished. See the introduction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.