Configuration of Data Connection Pool in SpringBook (tomcat,HikariCP,dbcp2,druid)

Posted by moltenice on Mon, 26 Aug 2019 14:04:38 +0200

Configuration of Data Connection Pool in SpringBook (tomcat,HikariCP,dbcp2,druid)

Articles Catalogue

1. Four connection pools supported in spring boot

By default, database connections can be automatically configured using the DataSource pool. The following is an algorithm for selecting a specific implementation:

  • Because of the performance and concurrency of Tomcat's data source connection pool, we always give preference to Tomcat when it is available.
  • If HikariCP is available, we will use it.
  • If Commons DBCP is available, we will use it, but it is not recommended in production environments.
  • Finally, if Commons DBCP2 is available, we will use it.

2. Configuration of Tomcat Data Source Connection Pool

Temporary space

3. HikariCP data source connection pool configuration

## Database Configuration
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
##  Hikari Connection Pool Configuration - Visit https://github.com/brettwooldridge/HikariCP for detailed configuration
## Minimum number of idle connections
spring.datasource.hikari.minimum-idle=5
## Maximum time for idle connections to survive, default 600000 (10 minutes)
spring.datasource.hikari.idle-timeout=180000
## Maximum number of connections in connection pool, default 10
spring.datasource.hikari.maximum-pool-size=10
## This property controls the default autocommit behavior of connections returned from the pool, with the default value of true:
spring.datasource.hikari.auto-commit=true
## Connection pool mother and child
spring.datasource.hikari.pool-name=MyHikariCP
## This property controls the longest lifetime of connections in the pool, with a value of 0 representing an infinite lifetime, defaulting to 1800000, or 30 minutes.
spring.datasource.hikari.max-lifetime=1800000
## Database connection timeout, default 30 seconds, or 30000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1

Copyright Statement: This article is the original article of CSDN blogger "I Jie That Scholar". It follows CC 4.0 by-sa Copyright Agreement. Please attach the link of origin and this statement for reproducing.
Links to the original text: https://blog.csdn.net/qq_32953079/article/details/81502237

4. dbcp2 data source reference configuration

# Data source configuration
spring.datasource.url=jdbc:mysql://localhost:3306/adzdb
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=acc
spring.datasource.password=acd
 
#Connection pool configuration
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=10000
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.validation-query=SELECT x
spring.datasource.dbcp2.connection-properties=characterEncoding=utf8

Reference Address: Copyright Statement: This is an original article of CSDN blogger "Yushencheng Master". It follows CC 4.0 by-sa Copyright Agreement. Please attach the link of origin and this statement for reproducing.
Links to the original text: https://blog.csdn.net/weixin_42749765/article/details/84879802

5. druid data connection pool configuration

1. Write configuration files for configuration
For details of configuration, please refer to https://blog.csdn.net/blueheart20/article/details/52384032.
2. Introduce druid-spring-boot-starter package for configuration

maven dependence

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

Configuration information:

# oracle database connection configuration
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@192.168.1.1:1521:ORCL
spring.datasource.username = admin
spring.datasource.password = 123456
# druid configuration
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.max-active=20
spring.datasource.druid.initial-size = 1
spring.datasource.druid.max-wait = 60000

Refer to https://blog.csdn.net/weixin_38187317/article/details/81562571 for specific configuration.

Remarks

1. Pay attention to mysql database connection using data connection pool 8-hour timeout problem

Topics: Spring Druid Tomcat JDBC