记录一下HikariCP与Druid配置
HikariCP
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&autoReconnect=true
username: xxxx
password: xxxx
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时间(毫秒),超过这个时长还没有可用的连接,则会抛出SQLException
minimum-idle: 5 # 最小连接数
maximum-pool-size: 20 # 最大连接数
auto-commit: true # 自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则会被释放(retired)
pool-name: DataSourceHikariCP # 连接池的名字
max-lifetime: 3600000 # 连接池的最大生命时长(毫秒),超时则会被释放(retired)
connection-test-query: SELECT 1
Druid
导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.20</version>
</dependency>
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
username: xxxx
password: xxxx
druid:
# 初始化连接池的连接数量
initial-size: 5
# 最小空闲连接数
min-idle: 5
# 最大活跃连接数
max-active: 20
# 获取连接等待超时的时间
max-wait: 30000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
# 验证连接的 SQL 查询
validation-query: SELECT 1
# 是否在空闲时检测连接
test-while-idle: true
# 是否在获取连接时检测连接
test-on-borrow: true
# 是否在返回连接时检测连接
test-on-return: false
# 是否缓存 preparedStatement
pool-prepared-statements: true
# 每个连接的最大缓存 prepared statement 数量
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的 filters
filters: stat,wall,log4j
# 配置 stat 拦截器的属性
stat:
merge-sql: true
slow-sql-millis: 5000
filters:配置为 stat,wall,log4j,表示启用统计、防火墙和日志过滤器。
stat:配置统计拦截器的属性,merge-sql 用于合并 SQL 语句,slow-sql-millis 用于定义慢 SQL 的阈值。
小心 wall 过滤器,它是一个安全性过滤器,可能会对某些 SQL 查询造成限制。如果你不需要此过滤器来加强安全性,可以考虑去掉,避免不必要的性能开销。