Nodejs Course

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 tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
MySQL Pooling Connections

Last accessed pages

  1. Get Lower, Higher, and Closest Number (5331)
  2. Understanding OOP - Object Oriented Programming (5144)
  3. jQuery Ajax - load() method (10776)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137563)
  5. Rotate HTML objects, Div, Span, images with jQuery (7991)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (252)
  2. Read Excel file data in PHP - PhpExcelReader (91)
  3. PHP Unzipper - Extract Zip, Rar Archives (76)
  4. The Four Agreements (75)
  5. The Mastery of Love (66)