Database must-know series: database connection pool and connection management

Author: Zen and the Art of Computer Programming

1 Introduction

Connecting to the database is the most important aspect of an application. Before this, developers needed to build, manage, maintain and optimize database connections themselves. With the development of distributed and microservice architecture, the number of application servers continues to increase, and the method of simply relying on the internal processing of connections in the program can no longer support the demand. In order to solve this problem, the connection pool (Connection Pool) came into being. The connection pool is to pre-establish several connections and put them into a pool when creating a connection. When a connection needs to be used, a record is taken out of the pool and returned to the pool after use. This avoids frequent connection creation, improves performance and reduces resource consumption. In addition, the connection pool can also effectively prevent database connection leaks. Connection pools are divided into two categories: static connection pools and dynamic connection pools. The static connection pool refers to pre-creating a specified number of database connections when the system starts, and then directly removing the connections from the pool for data interaction with each subsequent call; the dynamic connection pool automatically creates and destroys connections based on actual needs. Both connection pools have their own advantages and disadvantages, and are more suitable in different scenarios. This article will explain the basic principles, implementation methods, application scenarios and other aspects of database connection pooling. I hope that by reading this article, I can have a comprehensive understanding of database connection pooling and flexibly apply it to actual work.

2. Basic principles of connection pooling

2.1 Concept definition and classification

Connection pool is a technology that improves application system performance. It is mainly used to reduce the time required to create new connections while ensuring connection availability. The main functions are as follows:

  1. Reuse of database connections: Using connection pool technology, database connections can be reused to avoid additional overhead caused by frequent opening and closing of database connections. During business peak periods, the use of connection pools can improve the overall operating efficiency of the system and reduce resource waste.

  2. Improve database access speed: Because the connection pool caches the database connection, the more times it is reused, the faster it becomes. During low business hours, you will wait for a certain period of time after releasing the connection, but as long as the connection is requested again, it will be restored quickly.

  3. Improve system stability: The connection pool uses long connections, which can effectively avoid the problem of too large or too small number of connections caused by too many connections, and the connections will not time out.

  4. Controllability and monitoring capabilities: After using the connection pool, there is no need to pay attention to whether the database connections are normal, too many, or too few. You only need to ensure that each database connection can be effectively utilized. Monitor the connection status, total capacity and usage in the connection pool, and intuitively grasp the load, traffic and usage of the current connection pool to achieve accurate and real-time monitoring and management.

Based on the above four characteristics, connection pools can be divided into the following three types:

  1. Fixed-size connection pool: The maximum capacity of this type of connection pool is fixed. When the application is initialized, a fixed number of connections are added to the pool and returned to the pool after use. Typical representative products include Oracle's OCI and SQL Server's ODBC.

  2. Scalable connection pool: The maximum capacity of this type of connection pool is not fixed, and connections can be added or deleted at any time. When the connection pool is idle, the application does not need to wait. It can create a new connection and add it to the pool. When the number of connections reaches the upper limit, it will wait for other threads to return the connection before continuing to allocate. Typical representative products include Apache DBCP and C3P0.

  3. Pooled connection pool: The pooled connection pool can not only realize connection reuse, but also ensure the reliability of the connection. The principle is to configure multiple connections for the same database and first check whether there is an available connection in the pool before obtaining the connection. If no connection is available, a new connection will be created. The advantage is that under high concurrency conditions, good performance can still be obtained and connection availability is guaranteed. Typical representative products include Hibernate SessionFactory.

2.2 Implementation of connection pool

There are two main ways to implement connection pooling:

  1. Proxy Pattern: The connection pool is implemented by middleware (such as Nginx or HA Proxy), and the application only communicates with the proxy, not the database. In this mode, the client needs to obtain a connection through the proxy instead of directly establishing a connection with the database. After the proxy receives the client's request, it checks whether there is an available connection in the connection pool. If there is, it allocates it. If not, it creates a new connection and returns it to the client. In this mode, the client needs to establish a connection with the proxy and must close the connection after the request is completed.

  2. DataSource Pattern: Integrate the javax.sql.DataSource interface in the application program, and the application program communicates directly with the DataSource object instead of establishing a connection with the database. In this mode, the application needs to obtain the database connection through the DataSource object. DataSource provides a standard method to obtain a connection and encapsulates all connection details, including database driver, connection string, username and password, etc., simplifying the connection process. When the connection method in DataSource is executed for the first time, it will obtain or create a new connection from the connection pool and cache it in DataSource for subsequent requests. When a connection is idle for more than a certain period of time, it will be automatically recycled.

2.3 Application scenarios of connection pool

There are many application scenarios for connection pooling. The following common scenarios are listed below:

  1. JDBC data source: For programming languages ​​that support JDBC API, DataSource can provide a database connection pool function, reducing the number of database connection creation and destruction times, and effectively improving application performance.

  2. Spring Framework: Spring supports various ORM frameworks, including MyBatis, Hibernate, etc., which all need to access the database through DataSource, so you can use the DataSourceUtils tool class provided by Spring to create DataSource objects.

  3. Distributed environment: In a distributed environment, network delays and transmission failures between application servers may affect the stability of database connections, and the existence of the connection pool allows a certain number of database connections to be maintained at any time to avoid problems. A situation where one server has a large number of connections and another server has none.

  4. Web application server cluster: Web application server clusters are generally deployed on multiple physical machines, and due to the network interconnectivity between servers, short-term connection failures may occur in high-concurrency scenarios. The existence of the connection pool can ensure that the number of connections per server is limited to a reasonable value to avoid the situation where a single server has a large number of connections but other servers have no connections.

  5. Large concurrent access scenarios: For large-scale concurrent access scenarios, such as flash sales and other scenarios, the connection pool can effectively control the number of database connections to avoid the database being overwhelmed. In addition, the connection pool can also queue and limit concurrent access, effectively protecting database resources.

3. MySQL database connection pool - analysis of HikariCP implementation

3.1 Introduction to HikariCP

HikariCP (High-Performance JDBC Connection Pool) is a third-party open source database connection pool. It is a powerful pure Java library that connects to the database using the JDBC API and provides configuration, monitoring and management of the database connection pool. It is very lightweight and can improve the performance and responsiveness of database connections in various connection scenarios. HikariCP provides fast and simple configuration options, as well as the option of automatic machine learning configuration. It can be said that HikariCP is a database connection pool framework worth trying.

3.2 HikariCP Overview

3.2.1 Download and install HikariCP

Download address: https://github.com/brettwooldridge/HikariCP

git clone https://github.com/brettwooldridge/HikariCP.git

Import maven project:

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.7.2</version>
</dependency>

3.2.2 Configuring and using HikariCP

(1) Load configuration file

Configuration file name: hikari.properties

dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
dataSource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
dataSource.user=root
dataSource.password=<PASSWORD>
poolName=myPool
maximumPoolSize=10
connectionTimeout=30000
idleTimeout=600000
maxLifetime=1800000
minimumIdle=10

Notice:

  1. dataSourceClassName: database driver class name, here set to MySQL driver
  2. dataSource.url: database connection URL
  3. dataSource.user: database user name
  4. dataSource.password: database password
  5. poolName: connection pool name
  6. maximumPoolSize: The maximum number of connections allowed in the connection pool
  7. connectionTimeout: timeout for establishing a connection (milliseconds)
  8. idleTimeout: connection survival time, default 600000ms (ten minutes)
  9. maxLifetime: maximum connection life cycle, default 1800000ms (30 minutes)
  10. minimumIdle: minimum number of idle connections, default 10
(2) Java code configuration HikariCP
HikariConfig config = new HikariConfig("hikari.properties"); //加载配置文件
try {
  DataSource dataSource = new HikariDataSource(config); //创建数据源
  //... 使用数据源...
} catch (Exception e) {
  e.printStackTrace();
} finally {
  config.close();
}
(3) Spring Boot integrates HikariCP

The spring-boot-starter-data-jpa dependency introduces HikariCP:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 引入HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

application.properties sets database connection information:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=<PASSWORD>
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.min-idle=10

SpringBoot's yml configuration file sets database connection information:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false
    username: root
    password: admin
    hikari:
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
      min-idle: 10

Note :

  1. During use, HikariCP can modify database connection parameters through parameters. Such as: setNetworkTimeout() method, setConnectionTestQuery() method, setValidationTimeout() method, etc.
  2. During use, it is recommended to set the value of minimumIdle to be slightly smaller than the maximum number of connections to improve database connection utilization.

Guess you like

Origin blog.csdn.net/universsky2015/article/details/133385403