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

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

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

createConnection()createPool()
const mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'my-database'
});

connection.connect();

// Database queries here ...

connection.end();  // or: connection.destroy();

Example on GitHub is available here.

const mysql = require('mysql');

const pool  = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'my-database',
    connectionLimit: 10,
});

// Database single independent query here ...
// ...
// Database single independent query here ...
// ...
// Database single independent query here ...
// ...

// Close pool after all queries using:
//
pool.end();

Example on GirHub is available here.

 

3. Complex examples

createConnection()createPool()
const mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'my-database'
});

connection.connect((err) => {
    if (err) throw err;
    console.log('Database connected.');
});

// Database queries here ...

connection.end(function(err) {
    console.log('Connection closed.');
});

Example on GitHub is available here.

const mysql = require('mysql');

const pool  = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'my-database',
    connectionLimit: 10,
});

pool.getConnection((err, connection) => {
    if (err) throw err;
    console.log('Database connected.');
    //
    // Database related queries here ...
    // It means:             query 1
    //           related sub-query 2
    //           related sub-query 3
    //           ...
    // After all queres call:
    //           connection.release();
});

// Close pool after all queries using:
//
pool.end((err) => {
    if (err) throw err;
    console.log('Pool closed.');
});

Example on GirHub is available here.

 

References

  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
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