Principle and design of MySQL connection pool

1. What is a database connection pool?

Official: The
database connection pool is to establish enough database connections when the program starts, and these connections form a connection pool, and the program dynamically applies for, uses, and releases the connections in the pool.

Vernacular:
Creating a database connection is a very time-consuming operation and can easily cause security risks to the data. Therefore, when the program is initialized, multiple database connections are created centrally, and they are centrally managed for use by the program, which can ensure a faster database read and write speed, and is more secure and reliable.


2. Why do we need a database connection pool?

"" Look at the comparison of the two pictures first

----->
The network interaction required to execute a SQL statement without using the connection pool

Insert picture description here

Each time a SQL statement is executed, the network interactions include:
1) The three-way handshake of TCP connection establishment (the connection between the client and the mysql server is based on the TCP protocol).
2) Three-way handshake for MySQL authentication.
3) Real SQL execution.
4) Shut down MySQL.
5) Four waves of TCP close.

----->
Use connection pool scenario

Insert picture description here
A connection needs to be established for the first visit, but subsequent visits will reuse the previously created connection and execute the SQL statement directly.

" Advantages of using connection pools
1) Resource reuse
Because database connections are reused, the performance overhead caused by frequent creation and release of connections is avoided. On the basis of reducing system consumption, it also improves the stability of the system operating environment. (Reduce memory fragmentation and the number of temporary database processes/threads).

2) Faster system speed.
During the initialization of the database connection pool, several database connections are often created and placed in the pool for standby. At this time, the initialization of the connection has been completed. For business request processing, the existing available connections are directly used to avoid the overhead of the database connection initialization and release process, thereby reducing the overall response time of the system.

3) Unified connection management to avoid database connection leakage.
In the implementation of a relatively complete database connection pool, the occupied connection can be forcibly recovered according to the pre-set connection occupation timeout. This avoids resource leakage that may occur in conventional database connection operations.


Three, database connection pool operating mechanism

》》The operating mechanism of the connection pool
Insert picture description here

  • Obtain or create available connections from the connection pool.
  • After use, the connection is returned to the connection pool.
  • Before shutting down the system, disconnect all connections and release the occupied system resources.

》》The difference between connection pool and thread pool
Connection pool: passive use.
Thread pool: Actively and continuously perform tasks from the queue.

》》Elements required for MySQL connection
Host IP, host port, user name, and password.


Four, connection pool connection number setting

Calculation formula for reference: number of connections = (number of cores * 2) + number of effective disks


Five, the design idea of ​​the connection pool

》》The design idea of ​​the connection pool
1) Connecting to the database involves the host IP, host port, user name, password, and database name; each connection to the database is independent.
2) A queue management connection is required, such as list.
3) Request connection resources.
4) Return the connection resources.

》》What factors need to be considered when designing a good connection pool
1) Reconnection mechanism and statistics on reconnection times.
2) Statistics of the total number of connections.
3) The number of peak connections, such as counting once every 1, 5, and 15 seconds, and the peak number of connections for subsequent performance evaluation.
4) Timeout mechanism, blocking, non-blocking mode, etc.


Six, some key source code

》》Create multiple connections

int CDBPool::Init()
{
    
    
	for (int i = 0; i < conn_num; i++)
	{
    
    
		CDBConn *pDBConn = new CDBConn(this);
		int ret = pDBConn->Init();
		if (ret)
		{
    
    
			delete pDBConn;
			return ret;
		}

		m_conn_list.push_back(pDBConn);
	}
	return 0;
}

》》Connect to the database

CDBConn::CDBConn(CDBPool *pPool)
{
    
    
	m_pDBPool = pPool;
	m_mysql = NULL;
}

int CDBConn::Init()
{
    
    
	m_mysql = mysql_init(NULL);
	if (!m_mysql)
	{
    
    
		return 1;
	}

	my_bool reconnect = true;
	// 配合mysql_ping实现自动重连 
	//从MySQL 5.0.3开始默认情况下禁止再连接;这是5.0.13中的新选项,提供了一种以显式方式设置再连接行为的方法
	mysql_options(m_mysql, MYSQL_OPT_RECONNECT, &reconnect);	
	mysql_options(m_mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4");

	if (!mysql_real_connect(m_mysql, m_pDBPool->GetDBServerIP(), m_pDBPool->GetUsername(), m_pDBPool->GetPasswrod(),
							m_pDBPool->GetDBName(), m_pDBPool->GetDBServerPort(), NULL, 0))
	{
    
    
		return 2;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110354958