What is a database connection pool
Official explanation: The database connection pool is responsible for allocating, managing, and releasing database connections, allowing applications to reuse an existing database connection instead of re-establishing one, freeing up database connections that have been idle for more than the maximum idle time to avoid missing database connections due to not releasing the database connection. This technology can significantly improve the performance of database operations.
Personal Understanding: The database connection pool is a buffer between the user request and the database, because in the case of a large number of requests to the database, the frequent creation and shutdown of the connection will severely degrade the performance of the database and server, a buffer is needed to alleviate the burden on the database and the server.
Direct access to the database
Simple database requests each access to create a new connection runs out of close
Using connection pooling
Create a connection pool at the beginning of the program
Request to come in and return data to the request by connecting the pools response
The request is complete and the connection is returned to the connection pool
Disconnect frees resources when exiting a program
Using connection pooling can help us better manage database connections
Here we have the connection pool code for database creation as follows:
Const dbpool={ //Configuration Databaseconfig: {host:"Host Name", Port:"Port number", User:"Database user name", Password:"Database Password", Database:"Database Name"}, DbConnection:function(sqlstr, Sqlarr, fn) {//Create connection pool, pass in config, return linked objectLet Pool=mysql.createpool ( This. config); //get linked objectPool.getconnection (function(err,connection) {if(Err) {Console.log (connection)}//Send Queryconnection.query (sqlstr, Sqlarr, FN); //Close Linkconnection.release (); }) }}//make Dbpool public.Module.exports = Dbpool;
This is a personal understanding of node. js using the connection pool and simple way to establish a connection pool, there are shortcomings, welcome the big guy pointing
"Cicada Hall Learning Notes"--about the understanding and simple establishment of node. JS Access database connection pool [email protected]<