The application does not end with the MySQL pool

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

However, the following application does not exit - although I expect it to finish after calling connection.end()

An application works very well if I use one connection instead of a pool. Do I need to somehow terminate the pool?

Used library : 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"); }); 
+4
source share
2 answers

I had the 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 rotates the end of the call () for all connections.

It also accepts a callback function that will be called after all connections are completed (or whenever an error occurs).

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

I would use

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

Source: https://habr.com/ru/post/1482623/


All Articles