1. HikariCP

Spring Boot 默认使用数据库连接池 HikariCP,修改默认配置请修改 application.properties

# 数据库连接池 Hikari
spring.datasource.hikari.minimumIdle       = 5
spring.datasource.hikari.maximumPoolSize   = 50
spring.datasource.hikari.idleTimeout       = 30000
spring.datasource.hikari.maxLifetime       = 2000000
spring.datasource.hikari.connectionTimeout = 30000

可配置的属性有请参考 Configuration (knobs, baby!)。从日志中也可以看到相关配置:

DEBUG - HikariPool-1 - configuration:
DEBUG - allowPoolSuspension.............false
DEBUG - autoCommit......................true
DEBUG - catalog.........................none
DEBUG - connectionInitSql...............none
DEBUG - connectionTestQuery.............none
DEBUG - connectionTimeout...............30000
DEBUG - dataSource......................none
DEBUG - dataSourceClassName.............none
DEBUG - dataSourceJNDI..................none
DEBUG - dataSourceProperties............{password=<masked>}
DEBUG - driverClassName................."com.mysql.jdbc.Driver"
DEBUG - healthCheckProperties...........{}
DEBUG - healthCheckRegistry.............none
DEBUG - idleTimeout.....................30000
DEBUG - initializationFailTimeout.......1
DEBUG - isolateInternalQueries..........false
DEBUG - jdbcUrl.........................jdbc:mysql://localhost:3306/test...
DEBUG - leakDetectionThreshold..........0
DEBUG - maxLifetime.....................2000000
DEBUG - maximumPoolSize.................50
DEBUG - metricRegistry..................none
DEBUG - metricsTrackerFactory...........none
DEBUG - minimumIdle.....................5
DEBUG - password........................<masked>
DEBUG - poolName........................"HikariPool-1"
DEBUG - readOnly........................false
DEBUG - registerMbeans..................false
DEBUG - scheduledExecutor...............none
DEBUG - schema..........................none
DEBUG - threadFactory...................internal
DEBUG - transactionIsolation............default
DEBUG - username........................"root"
DEBUG - validationTimeout...............5000

2. Druid

使用连接池 Druid 需要引入依赖 implementation 'com.alibaba:druid-spring-boot-starter:1.1.14',以及 application.properties 中配置

# 数据库连接池 Druid
spring.datasource.druid.initial-size                      = 2
spring.datasource.druid.max-active                        = 150
spring.datasource.druid.min-idle                          = 20
spring.datasource.druid.max-wait                          = 60000
spring.datasource.druid.pool-prepared-statements          = true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size = 20
spring.datasource.druid.max-open-prepared-statements      = 20
spring.datasource.druid.validation-query                  = SELECT 1 FROM DUAL
spring.datasource.druid.validation-query-timeout          = 2000
spring.datasource.druid.test-on-borrow                    = false
spring.datasource.druid.test-on-return                    = false
spring.datasource.druid.test-while-idle                   = true
spring.datasource.druid.time-between-eviction-runs-millis = 60000
spring.datasource.druid.min-evictable-idle-time-millis    = 300000
spring.datasource.druid.max-evictable-idle-time-millis    = 300000

更多帮助请参考 https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

MySQL 查看活跃连接数: SHOW PROCESSLIST

3. Hikari 还是 Druid ?

如无特殊需求,例如监控 SQL 的执行情况 (可选 Druid),Hikari 就可以了。

results matching ""

    No results matching ""