Rather than creating and managing connections one-by-one, "mysql" module also provides
built-in connection pooling using
mysql.createPool(config) method.
A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required.
Connection pools are used to enhance the performance of executing commands on a database.
In connection pooling, after a connection is created, it is placed in the pool and it is used again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool.
Pools accept all the same options as a connection. When creating a new connection, the options are simply passed to the connection constructor.
Example of using pool directly:
const mysql = require('mysql');
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'pass',
database: 'nodedb',
charset: 'utf8'
});
let sql ='SELECT 1 + 1 AS solution';
pool.query(sql, (err, res, fields)=>{
if(err) throw err;
console.log('The solution is: ', res[0].solution);
});
- Call the
pool.getConnection() method to use a connection from pool.
When you are done with a connection, just call
con.release();, and the connection will return to the pool, ready to be used again by someone else.
const mysql = require('mysql');
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'pass',
database: 'nodedb',
charset: 'utf8'
});
//use pool connection
var sql ='SELECT 1 + 1 AS solution';
pool.query(sql, (err, res, fields)=>{
if(err) throw err;
console.log('The solution is: ', res[0].solution);
});
//another connection
pool.getConnection((err, con)=>{
sql ='SELECT id, name FROM friends LIMIT 1';
// Use the connection
con.query(sql, (err, res, fields)=>{
console.log(res[0]);
con.release(); //done with the connection, free memory
// Handle error after the release
if(err) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});
Results:
The solution is: 2
{id: 1, name: 'Olpram'}
- If you would like to close the connection and remove it from the pool, use
con.destroy(); instead. The pool will create a new connection the next time
getConnection() is called.
- For more details and examples, see documentation:
Node.js MySQL Pooling connections.
Daily Test with Code Example
HTML
CSS
JavaScript
PHP-MySQL
Which type of <input> creates a date input control, such as a pop-up calendar?
type="text" type="date" type="button"<input type="date" name="set_date" value="2012-10-15" />
Which CSS property adds shadow effects to the text of an element?
font-style color text-shadowh2 {
text-shadow: 2px 3px 3px #a0a1fe;
}
Click on the function that adds new elements to the end of an array.
pop() shift() push()var pags = ["lessons", "courses"];
pags.push("download", "tutorials");
alert(pags[2]); // download
Which function sorts an array by key, in ascending order, maintaining key to data correlations?
asort() ksort() sort()$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"coursesweb.net");
ksort($lang);
var_export($lang); // array ("site"=>"coursesweb.net", 10=>"PHP", 20=>"JavaScript")