Official explanation: The database connection pool is a sufficient database connection when the program is started. The database connection in the connection pool is dynamically used to apply, use and recycle the database connection in the connection pool.
Personal understanding: Establishing a database connection is a time -consuming operation. Through the database connection pool, applying for a sufficient connection to the database through the database connection pool. When you need to use it, do you need to establish a connection from the database. The database operation is waiting to establish a connection time, and the connection pool is recycled after the connection is completed, achieving the effect of connection and reuse.
Each operation requires application to establish a connection with the database, and release the database connection directly after use, but it is more expensive to create a database connection that needs to be consumed. It is easy to cause database collapse.

Work steps of connecting pool:
key parameter:
From the above content, it is not difficult to see that the connection pool is actually the cache of the database connection of the program maintenance so that the connection can be reused when the database is required in the future. The connection pool is used to improve the performance of executing commands on the database. It is expensive and wasteful for each user to open and maintain database connection, especially for dynamic database -driven applications. In the connection pool, after creating a connection, the connection is placed in the pool and reused, so that there is no need to create a new connection. If all connections are in use, create a new connection and add it to the pool. The connection pool also reduces the time when the user must wait for the connection with the database, and by setting the maximum number of connections to protect the database, to prevent excessive number of connections and the database becomes slow or even collapsed.
At present, the most commonly used ORM frameworks such as Xorm and Gorm are supported by the connection pools. Next, Xorm will take an example to write a small DEMO using the connection pool to operate mysql.
func InitMysqlHandler(conf MysqlConfig) error {
var (
err error
)
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", conf.Username, conf.Password, conf.Host, conf.Port, conf.DBName)
engine, err := xorm.NewEngine("mysql", dataSource)
if err != nil {
return err
}
engine.ShowSQL(true)
engine.ShowExecTime(true)
engine.SetMaxOpenConns(conf.MaxOpenConn) // Set the maximum number of connecting connections to the connection pool
engine.SetMaxIdleConns(conf.MaxIdleConn) // Set the maximum free connection number of connection pools
engine.SetConnMaxLifetime(10 * time.Second) // Set the maximum reuse time
engine.SetMapper(core.LintGonicMapper)
err = engine.Ping()
if err != nil {
return err
}
mysqlHandler = &MysqlHandler{
engine: engine,
}
return nil
}
XORM opens the connection pool by default, and learn from the source code
// SetMaxIdleConns set the max idle connections on pool, default is 2 func (engine *Engine) SetMaxIdleConns(conns int) { engine.db.SetMaxIdleConns(conns) }The maximum number of idle connections by default is 2
func NewSession() *xorm.Session { return mysqlHandler.engine.NewSession() }Every time you need to perform a DB operation, engine.newsession () is to use a connection from the connection pool to use it. Remember to call session.close () to return to the pool.
This article introduces the concept of the database connection pool, and briefly introduces the definition and use of the connection pool by using the Go Xorm framework. Welcome to megithub Check the source code in the warehouse. If you have any questions, you can discuss the comment area.
Overview of connection pool Manage database connection The role of database connection pool Improve project performance does not use connection pool technology, every operation needs to be connected o...
Article Directory 1. Prerequisite preparation: 1.1 Create a database 1.2 Create an entity class object 1.3 JDBC tools 2. Encapsulate data into entity classes based on jdbc tool class 3. The use of c3p...
My blog address:http://www.ypbck.xyz http://118.24.14.224 Take mysql as an example, the database connection defaults to 8 hours, which means that if a connection is not used f...
1. How to access the downstream through the connection There are many requirements for accessing downstream in the engineering architecture. The downstream includes but is not limited to service / dat...
What is a connection pool? No connection pool Database connection pool Pool connection pool effect Database connection pool schematic diagram Attributes in the connection pool Connection pool use The ...
one. Copy the database connector .jar package to tomcat$\common\lib, because it is useless under the project \WEB-INF\lib, it is managed by tomcat two. Tomcat\conf\servlet.xml, below is the personal p...
Connection factory ConnectionFactory Data connection agent class_Connection Connection parameter class ConnectionParam Test class ...
Overview At present, the database connection pool products are very numerous, DBCP, C3P0, Proxool, etc. are very excellent products. The performance and stability of the connection pool can have a sig...
SUN provides an interface for implementing connection pooling: javax.sql.DataSource, which requires all connection pools to implement this interface. Steps to implement a database connection pool 1. W...
The database connection pool in Java is not necessarily related to JNDI. In fact, the connection pool is the initial number of Connections, and then the application can use the connection pool to obta...