mysql.createConnection vs mysql.createPool in Node JS
mysql.createConnection
When you create a connection with mysql.createConnection, you only have one connection, and it lasts until you close it OR connection closed by MySQL.
A single connection is blocking. While executing one query, it cannot execute others. Hence, your application will not perform good.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'mydb',
});
Suppose we have an only ONE road to reach to our destination then it is the only way to go and if any traffic jam occur we don’t have other options. That is exactly createConnection.
mysql.createPool
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.
In a pool while one connection is busy running a query, others can be used to execute subsequent queries. Hence, your application will perform good.
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password : '',
database : 'mydb',
});
Suppose we have a highway which have multiple way to reach to our destination, then we take the path that is more free or available with less traffic, that exactly is createPool.
Thanks for reading…
Keep Coding…
Keep Exploring…