Languages
[Edit]
EN

Node.js / MySQL - createConnection vs createPool

3 points
Created by:
Jan-Alfaro
681

In this article, we would like to show you differences between mysql.createConnection() and mysql.createPool() in Node.js.

1. Quick overview

Edit

Both createConnection() and createPool() methods provided by the mysql module are being used for establishing a connection to a MySQL database. However, there are some differences in terms of how they handle database connections.

createConnection()createPool()
Establishes a single connection to the MySQL database.Creates a pool of database connections, which can be used and reused as needed.

The connection is kept open until:

  • unexpected disconnection,
  • mysql timeout,
  • explicitly using the end() method.
Connections from the pool are acquired and released automatically by the mysql module.
This method is suitable for applications that require only one or a few database connections.This method is suitable for applications that require a large number of database connections, such as high-traffic web applications.

 

2. Simple examples

Edit

In the table below, you can see practical examples of how to use createConnection() and createPool() methods in Node.js.

createConnection()createPool()

Example on GitHub is available here.

Example on GirHub is available here.

 

3. Complex examples

Edit
createConnection()createPool()

Example on GitHub is available here.

Example on GirHub is available here.

 

References

Edit
  1. mysql - npm Docs
  2. mysql - GitHub

Alternative titles

  1. Node.js - mysql.createConnection() vs mysql.createPool()
  2. Node.js / MySQL - createConnection vs createPool (database connection)
  3. Node.js / MySQL - connecting to database - createConnection vs createPool
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join