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.
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. |
In the table below, you can see practical examples of how to use createConnection()
and createPool()
methods in Node.js.
createConnection() | createPool() |
---|---|
xxxxxxxxxx 1 const mysql = require('mysql'); 2 3 const connection = mysql.createConnection({ 4 host: 'localhost', 5 user: 'root', 6 password: 'password', 7 database: 'my-database' 8 }); 9 10 connection.connect(); 11 12 // Database queries here ... 13 14 connection.end(); // or: connection.destroy(); Example on GitHub is available here. |
xxxxxxxxxx 1 const mysql = require('mysql'); 2 3 const pool = mysql.createPool({ 4 host: 'localhost', 5 user: 'root', 6 password: 'password', 7 database: 'my-database', 8 connectionLimit: 10, 9 }); 10 11 // Database single independent query here ... 12 // ... 13 // Database single independent query here ... 14 // ... 15 // Database single independent query here ... 16 // ... 17 18 // Close pool after all queries using: 19 // 20 pool.end(); Example on GirHub is available here. |
createConnection() | createPool() |
---|---|
xxxxxxxxxx 1 const mysql = require('mysql'); 2 3 const connection = mysql.createConnection({ 4 host: 'localhost', 5 user: 'root', 6 password: 'password', 7 database: 'my-database' 8 }); 9 10 connection.connect((err) => { 11 if (err) throw err; 12 console.log('Database connected.'); 13 }); 14 15 // Database queries here ... 16 17 connection.end(function(err) { 18 console.log('Connection closed.'); 19 }); Example on GitHub is available here. |
xxxxxxxxxx 1 const mysql = require('mysql'); 2 3 const pool = mysql.createPool({ 4 host: 'localhost', 5 user: 'root', 6 password: 'password', 7 database: 'my-database', 8 connectionLimit: 10, 9 }); 10 11 pool.getConnection((err, connection) => { 12 if (err) throw err; 13 console.log('Database connected.'); 14 // 15 // Database related queries here ... 16 // It means: query 1 17 // related sub-query 2 18 // related sub-query 3 19 // ... 20 // After all queres call: 21 // connection.release(); 22 }); 23 24 // Close pool after all queries using: 25 // 26 pool.end((err) => { 27 if (err) throw err; 28 console.log('Pool closed.'); 29 }); Example on GirHub is available here. |