Mysql: How to close pool and release all the connection in pool?

Created on 24 Apr 2016  路  5Comments  路  Source: mysqljs/mysql

Is it possible?
If not, where should I add this functionality in the source ccode?

question

Most helpful comment

Hi @AminaG, in your example, since you are simply using pool.query for every operation, just calling pool.end would close them all. Extending your example:

var pool=db.createPool({})
for(var i=1;i<100;i++)
pool.query()

// Now I want to close all the connection of the pool
pool.end()

All 5 comments

"Releasing" only has meaning in the context of a pool. If you don't need a pool and you want to gracefully close all connections belonging to that pool, just use pool.end()

https://github.com/felixge/node-mysql/blob/1720920f7afc660d37430c35c7128b20f77735e3/lib/Pool.js#L152-L182

I use a pool, and then the app decide to close the pool.
For example:

var pool=db.createPool({})
for(var i=1;i<100;i++)
pool.query()

// Now I want to close all the connection of the pool

Hi @AminaG, in your example, since you are simply using pool.query for every operation, just calling pool.end would close them all. Extending your example:

var pool=db.createPool({})
for(var i=1;i<100;i++)
pool.query()

// Now I want to close all the connection of the pool
pool.end()

Hello,

Is it possible to wait that all waiting queries are completed before closing the pool please?

Regards,
S

Ended up with:

        _shouldWeStopChild() {
            return new Promise((resolve, reject) => {
                setInterval(() => {
                    // Ensure all queries has been executed before exiting the script
                    let connectionQueue = this.Database.getCurrentConnection()._connectionQueue.length;
                    if(connectionQueue == 0) {
                        clearInterval(this._shouldWeStopChild);
                        debug('All queries has been completed.');
                        resolve();
                    } else {
                        debug('There are ' + connectionQueue + ' queries left');
                    }
                }, 50);
            });
        }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

flowl picture flowl  路  4Comments

macias picture macias  路  3Comments

ajpyoung picture ajpyoung  路  4Comments

whatthehell232 picture whatthehell232  路  3Comments

EdoardoPedrotti picture EdoardoPedrotti  路  3Comments