30

where do i close the mysql connection?

I need to run queries in sequence. I am writing code that looks like this at present:

var sqlFindMobile = "select * from user_mobiles where mobile=?";
var sqlNewUser = "insert into users (password) values (?)";
//var sqlUserId = "select last_insert_id() as user_id";
var sqlNewMobile = "insert into user_mobiles (user_id, mobile) values (?,?)";
connection.connect(function(err){});
var query = connection.query(sqlFindMobile, [req.body.mobile], function(err, results) {
    if(err) throw err;
    console.log("mobile query");
    if(results.length==0) {
        var query = connection.query(sqlNewUser, [req.body.password], function(err, results) {
            if(err) throw err;
            console.log("added user");
            var user_id = results.insertId;
            var query = connection.query(sqlNewMobile, [user_id, req.body.mobile], function(err, results) {
                if(err) throw err;
                console.log("added mobile");
                    //connection.end();
                });
        });
    }
});
//connection.end();

(I am a beginner with node, npm-express and npm-mysql. I have tried searching SO for "express mysql cannot enqueue" to find related questions and have not found them.)

Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
  • you should either move your connection.end() call inside the callbacks, or not have a connection.end() call at all if the connection is opened on program start. check this answer https://stackoverflow.com/a/16134471/8111190 – Khubaib Raza Aug 07 '20 at 07:55

4 Answers4

36

I fixed this problem use this method:

connection.end() in your connection.query function

The fixed code is here

r0-
  • 2,388
  • 1
  • 24
  • 29
unlimited
  • 471
  • 3
  • 10
11

If you're using the node-mysql module by felixge then you can call connection.end() at any point after you've made all of the connection.query() calls, since it will wait for all of the queries to finish before it terminates the connection.

See the example here for more information.

If you're wanting to run lots of queries in series, you should look into the async module, it's great for dealing with a series of asynchronous functions (i.e. those that have a callback).

matthewtole
  • 3,207
  • 22
  • 19
  • I am calling connection.end() the one that is commented out after the "added mobile" log. the problem i am sensing is that if the flow exits that i.e. in any of the throw(err) points, the the connection will not be closed. i am painfully aware of connections being left open a rookie mistake and its effect on scalability. i just dont want to do that at this point in my life. so if you have a pattern that could be followed for this, that would be great. async seems to be a good call. i will take a day to check it out and respond to it. thanks. – Kinjal Dixit Jan 15 '13 at 17:26
  • spoken to soon, async would not work for me because these queries are nested, and not something that has to be executed in series. maintaining states and checking preconditions is too much overhead for this. – Kinjal Dixit Jan 15 '13 at 17:34
  • You can use the waterfall function to run the queries. I've written out the basic structure here: http://pastebin.com/RDaiqZsp – matthewtole Jan 15 '13 at 17:39
5

Maybe the problem is that the mySQL query is executed after the connection is already closed, due to the asynchronous nature of Node. Try using this code to call connection.end() right before the thread exits:

function exitHandler(options, err) {
    connection.end();
    if (options.cleanup)
        console.log('clean');
    if (err)
        console.log(err.stack);
    if (options.exit)
        process.exit();
}

//do something when app is closing
process.on('exit', exitHandler.bind(null, {cleanup: true}));

Code adapted from @Emil Condrea, doing a cleanup action just before node.js exits

Community
  • 1
  • 1
user3413723
  • 11,147
  • 6
  • 55
  • 64
1

In my case connection.end was being called in a spot that was hard to notice, so an errant call to connection.end could be the problem with this error

Kyle Roux
  • 736
  • 5
  • 11