yas4891
yas4891

Reputation: 4862

Application does not terminate with MySQL pool

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");
});

Upvotes: 4

Views: 3380

Answers (2)

Kalmh
Kalmh

Reputation: 1

I would use

getCampaignData(function(err, rows, connection)
{
    if (err) throw err;
    connection.release();
    console.log("I expect my app to terminate");
});

Upvotes: 0

saverio.caminiti
saverio.caminiti

Reputation: 61

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");
});

Upvotes: 3

Related Questions