What is the database connection pool?

tags: database  mysql  

Mysql connection pool

concept

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.

Two connections

  • Establish a connection directly with the database:

 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.

  • Use the connection pool mechanism:

Work steps of connecting pool:

  1. Set the parameters, involve the maximum free connection, the maximum number of connections, and the maximum reuse time.
  2. When the program starts, the connection pool applies to the database available connection, and the number is the maximum number of idle connections.
  3. The business threads apply for connection to the connection pool and return to the connection pool after use.
  4. When the program exits, disconnect all connections.

key parameter:

  • The maximum number of idle connections: The number of connections that the connection pool keeps keeps, generally the default is 2. If the default is used, the connection pool will apply to the database to apply for two connections and keep it.
  • The maximum number of connections: the number of connections that the thread pool can apply for, the default is not limited (Be sure to specify in the production environment), When the number of applications for the program to the thread pool exceeds the maximum idle connection, if you apply at this time, the equivalent is to build a new connection to the database through the thread pool, but this connection will not be released immediately, but will be recovered. use. When the number of connections apply for the program is greater than the maximum number of connections, the subsequent requests will be put in the waiting queue to wait for the connection of other threads.
  • The maximum reuse time of the connection: This parameter is used to the above request connection more than the maximum idle connection. The additional application connection will not be released immediately. After the business thread is completed The waiting time is the maximum reuse time. After waiting for this time, the connection will be destroyed.

What exactly is the connection pool?

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.

Code practice

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.

Summarize

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.

Intelligent Recommendation

Custom database connection pool and database connection pool

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...

Database connection pool c3p0 database connection pool

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...

What happens to the program connection failure caused by the database connection failure - mybatis database connection pool configuration" database connection failure causes program exceptions how to do -- mybatis database connection pool configuration

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...

What is a connection pool

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...

-JavaWeb what is the connection pool and connection pool use

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 ...

More Recommendation

Tomcat database connection pool

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...

Database connection pool

Connection factory ConnectionFactory       Data connection agent class_Connection     Connection parameter class ConnectionParam       Test class    ...

Database connection pool configuration

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...

Handwriting a database connection pool

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...

Java database connection pool

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top