Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of unused/used connection in nodejs mysql connection pool?

I am using nodejs connection pooling, with npm's "mysql" module. While creating a pool I have specified the connectionLimit as 100. I would like to know how many of my connections are used/unused from the pool at runtime.

like image 470
Rohit Nayal Avatar asked Feb 04 '16 07:02

Rohit Nayal


People also ask

How do you use Connection pool in node JS?

The Node. js driver supports connection pooling. Connection pooling allows your application to reuse existing connections by automatically saving the connection to a pool so it can be reused, rather than repeatedly creating a new connection to the SAP HANA database server.

How many connection pools should I have?

For optimal performance, use a pool with eight to 16 connections per node. For example, if you have four nodes configured, then the steady-pool size must be set to 32 and the maximum pool size must be 64.

How do I count rows in node JS?

js MySQL Count() Function. We use the COUNT() function in MySQL to count a number of rows in a column satisfying the WHERE clause. Parameters: COUNT() function accepts a single parameter as mentioned above and described below.

What is connection limit in node JS?

If connectionLimit is 10, the maximum number of connections that the driver will make to the database is 10.


Video Answer


1 Answers

By looking at the source code here, it appears that you can look at:

pool.config.connectionLimit     // passed in max size of the pool
pool._freeConnections.length    // number of free connections awaiting use
pool._allConnections.length     // number of connections currently created, including ones in use
pool._acquiringConnections.length // number of connections in the process of being acquired

Note: New connections are created as needed up to the max size of the pool so _freeConnections.length could be zero, but there are many more connections in the limit so the next time .getConnection() is called, it will create a new connection.

like image 151
jfriend00 Avatar answered Oct 20 '22 18:10

jfriend00