Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between mysql.createConnection and mysql.createPool in Node.js MySQL module?

Tags:

I am trying to understand the difference between

mysql.createConnection

var mysql = require('mysql');

var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

and

mysql.createPool

var mysql = require('mysql');

var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret' 
});

in the mysql module for Node.js.

I am already familiar with mysql.createConection. It seems this is the default way to establish a connection with MySQL.

What exactly does mysql.createPool do?

When do I start using mysql.createPool?

What are the benefits of using mysql.createPool as opposed to mysql.createConnection?

like image 757
user3658794 Avatar asked Oct 17 '14 19:10

user3658794


People also ask

What is the difference between createPool and Createconnection?

When you create a connection, you only have one connection and it lasts until you close it (or it is closed by the mysql server). You can pass it around by reference and re-use it, or you can create and close connections on demand. A pool is a place where connections get stored.

What does createPool do in MySQL?

mysql. createPool is a place where connections get stored. When you request a connection from a pool,you will receive a connection that is not currently being used, or a new connection. If you're already at the connection limit, it will wait until a connection is available before it continues.

Which module is required to connect to MySQL using node?

To access a MySQL database with Node. js, you need a MySQL driver. This tutorial will use the "mysql" module, downloaded from NPM. Now you have downloaded and installed a mysql database driver.


1 Answers

When you create a connection, you only have one connection and it lasts until you close it (or it is closed by the mysql server). You can pass it around by reference and re-use it, or you can create and close connections on demand.

A pool is a place where connections get stored. When you request a connection from a pool, you will receive a connection that is not currently being used, or a new connection. If you're already at the connection limit, it will wait until a connection is available before it continues. These pooled connections do not need to be manually closed, they can remain open and be easily reused.

Which you use is entirely up to you, as they both accomplish the same goal, just in two different ways.

like image 98
Kevin B Avatar answered Sep 24 '22 01:09

Kevin B