SoFunction
Updated on 2025-04-13

Detailed explanation of connection pool (HikariCP, Druid)

1. Introduction

In database operations, establishing a database connection is a relatively time-consuming and resource-consuming process.

Reestablishing a connection every time a database operation will seriously affect the system performance. In order to solve this problem, connection pooling technology came into being.

The connection pool can create a certain number of database connections in advance and manage these connections. When an application needs to use a database connection, it is directly obtained from the connection pool and returned to the connection pool after use, avoiding the performance overhead caused by frequent creation and destruction of connections.

2. The basic principle of connection pool

The basic working principle of connection pooling is as follows:

  1. initialization: When the application starts, the connection pool creates a certain number of database connections according to the configuration information and stores these connections in the connection pool.
  2. Get the connection: When the application needs to perform database operations, an available database connection is requested from the connection pool. The connection pool will take a connection from the list of free connections to provide it to the application.
  3. Use connection: The application uses the obtained connection to perform database operations.
  4. Return the connection: After the application completes the database operation, the connection is returned to the connection pool. The connection pool marks the connection as idle for next use.
  5. Connection Management: The connection pool will manage the connection, including connection validity check, timeout processing, connection creation and destruction, etc.

3. HikariCP

3.1 Introduction

  • HikariCP is a high-performance JDBC connection pool developed by Japanese programmer Brett Wooldridge.
  • It has received widespread attention for its fast, lightweight and low latency features, and is used as the default connection pool by frameworks such as Spring Boot.

3.2 Advantages

  • high performance:HikariCP adopts an optimized bytecode and lock mechanism, reducing the overhead of lock competition and context switching, thereby improving the acquisition and release speed of connections.
  • Lightweight: Small code, low memory usage, fast startup speed, and less consumption of system resources.
  • Simple and easy to use: Simple configuration, only a small number of configuration parameters are required to meet the needs of most scenarios.

3.3 Configuration Example

Here is a simple configuration example using HikariCP:

import ;
import ;
import ;
import ;
import ;

public class HikariCPExample {
    public static void main(String[] args) {
        // Create HikariCP configuration object        HikariConfig config = new HikariConfig();
        ("jdbc:mysql://localhost:3306/mydb");
        ("root");
        ("password");
        (10); // Maximum number of connections        (5); // Minimum number of idle connections
        // Create HikariCP data source        HikariDataSource dataSource = new HikariDataSource(config);

        try (Connection connection = ();
             Statement statement = ();
             ResultSet resultSet = ("SELECT * FROM users")) {
            while (()) {
                (("username"));
            }
        } catch (Exception e) {
            ();
        } finally {
            // Close the data source            ();
        }
    }
}

IV. Druid

4.1 Introduction

Druid is an open source JDBC connection pool by Alibaba. It not only provides high-performance connection pooling functions, but also integrates monitoring, defense against SQL injection and other functions. It is a comprehensive database connection pooling solution.

4.2 Advantages

  • high performance: Similar to HikariCP, Druid also has high performance, reducing the acquisition and release time of connections through optimized connection pooling algorithms and efficient resource management.
  • Powerful monitoring function:Druid provides rich monitoring functions, which can monitor the status of the connection pool, SQL execution status, slow SQL statistics and other information in real time, making it convenient for developers to perform performance tuning and problem investigation.
  • Defense against SQL injection:Druid has a built-in SQL firewall, which can check and filter SQL statements, effectively preventing SQL injection attacks.

4.3 Configuration Example

Here is a simple configuration example using Druid:

import ;
import ;
import ;
import ;

public class DruidExample {
    public static void main(String[] args) {
        // Create a Druid data source        DruidDataSource dataSource = new DruidDataSource();
        ("jdbc:mysql://localhost:3306/mydb");
        ("root");
        ("password");
        (10); // Maximum number of connections        (5); // Minimum number of idle connections
        try (Connection connection = ();
             Statement statement = ();
             ResultSet resultSet = ("SELECT * FROM users")) {
            while (()) {
                (("username"));
            }
        } catch (Exception e) {
            ();
        } finally {
            // Close the data source            try {
                ();
            } catch (Exception e) {
                ();
            }
        }
    }
}

5. Comparison between HikariCP and Druid

5.1 Performance

  • In most cases, HikariCP has slightly better performance than Druid, especially in high concurrency scenarios, HikariCP has faster response speed and lower resource consumption.
  • But Druid also has excellent performance and can meet the needs of most business scenarios.

5.2 Functional aspects

  • HikariCP mainly focuses on providing high-performance connection pooling functions, with relatively simple functions;
  • In addition to the basic connection pooling function, Druid also provides powerful monitoring and security protection functions, which is more suitable for scenarios with high requirements for database monitoring and security.

5.3 Configuration

  • The configuration of HikariCP is relatively simple, and only requires configuring some basic parameters;
  • Druid's configuration is relatively complex because it provides more functional options, but it also makes its configuration more flexible.

6. Summary

HikariCP and Druid are excellent JDBC connection pools, and they have their own characteristics in terms of performance, functionality and configuration.

If your application requires extremely high performance and does not require complex monitoring and security features, then HikariCP is a good choice; if your application requires comprehensive monitoring and protection of the database and can also accept relatively complex configurations, then Druid will be more suitable for you.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.