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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With