How to add connection pooling with Node.js and MySQL?

Sometimes, we want to add connection pooling with Node.js and MySQL.

In this article, we’ll look at how to add connection pooling with Node.js and MySQL.

How to add connection pooling with Node.js and MySQL?

To add connection pooling with Node.js and MySQL, we call pool.getConnection to get a connection.

For instance, we write

const getConnection = (callback) => {
  pool.getConnection((err, connection) => {
    callback(err, connection);
  });
};

to define the getConnection function that takes the callback we want to call after the pool.getConnection is done.

We get the MySQL connection from the connection parameter.

And we call callback with the connection so we can use it elsewhere.

Then when we’re done, we call connection.release(); to release the connection.

Conclusion

To add connection pooling with Node.js and MySQL, we call pool.getConnection to get a connection.