4

I am writing a nodejs application and want to use connection pooling.

However, the following application does not terminate - although I would expect it to terminate after the call to connection.end()

Application works just fine, if I use one connection instead of the pool. Do I need to terminate the pool in some way?

Library used: https://github.com/felixge/node-mysql
node.js version: 0.10.4 on Ubuntu

var mysql      = require('mysql');
var pool = mysql.createPool({
    host     : 'example.org',
    user     : 'myuser',
    password : 'youbet',
    database : 'notrevealingdetails',
    insecureAuth: true
});

function getCampaignData(callback)
{
    pool.getConnection(function(err, connection) {
        if(err) throw err;

        connection.query(
            'SELECT cam.id, cam.name AS campaign_name, cam.subdomain, usr.email, usr.display_name AS user_displayname ' +
            'FROM campaigns AS cam INNER JOIN users AS usr ON usr.id = cam.user_id ' +
            'WHERE cam.state=2',
            function(err, rows) {
                callback(err, rows,connection);
                //console.log('called end()');
        }); // callback function for connection.query
    }); // end pool.GetConnection
}

getCampaignData(function(err, rows, connection) {
    if (err) throw err;


    connection.end();
    console.log("I expect my app to terminate");
});
yas4891
  • 4,774
  • 3
  • 34
  • 55
  • Try using connection.destroy() instead – spotirca May 24 '13 at 20:22
  • @spotirca `connection.destroy()` will (according to the documentation) kill the connection. If I do that, there's no point in using a pool in the first place - right? (**But it works**) – yas4891 May 24 '13 at 20:49
  • And by "it works" I mean: The application terminates. I still would like a way to make the pool work – yas4891 May 25 '13 at 01:21

2 Answers2

3

I was having the very same problem, but looking at the source code https://github.com/felixge/node-mysql/blob/master/lib/Pool.js I found that the pool, at least in its current implementation, has an end() method that is turns call end() on all connections.

It also accept a callback function to be called after all connections are actually ended (or whenever an error occur).

pool.end(function (err) {
    if (err) console.error("An error occurred: " + err);
    else console.log("My app terminated");
});
0

I would use

getCampaignData(function(err, rows, connection)
{
    if (err) throw err;
    connection.release();
    console.log("I expect my app to terminate");
});
nobalG
  • 4,544
  • 3
  • 34
  • 72
Kalmh
  • 1