EN
Node.js / MySQL - createConnection vs createPool
3
points
In this article, we would like to show you differences between mysql.createConnection()
and mysql.createPool()
in Node.js.
1. Quick overview
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:
| 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
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
createConnection() | createPool() |
---|---|
Example on GitHub is available here. |
Example on GirHub is available here. |