相关文章推荐
maxIdle 始终保留在池中的最大连接数,如果启用,将定期检查限制连接,超出此属性设定的值且空闲时间超过minEvictableIdleTimeMillis的连接则释放 与maxActive设定的值相同 minIdle 始终保留在池中的最小连接数,池中的连接数量若低于此值则创建新的连接,如果连接验证失败将缩小至此值 与initialSize设定的值相同 initialSize 连接池启动时创建的初始连接数量 maxWait 最大等待时间(毫秒),如果在没有连接可用的情况下等待超过此时间,则抛出异常 30000(30秒) testOnBorrow 当从连接池中取出一个连接时是否进行验证,若验证失败则从池中删除该连接并尝试取出另一个连接 false testOnConnect 当一个连接首次被创建时是否进行验证,若验证失败则抛出 SQLException 异常 false testOnReturn 当一个连接使用完归还到连接池时是否进行验证 false testWhileIdle 对池中空闲的连接是否进行验证,验证失败则回收此连接 false validationQuery 在连接池返回连接给调用者前用来对连接进行验证的查询 SQL validationQueryTimeout SQL 查询验证超时时间(秒),小于或等于 0 的数值表示禁用 timeBetweenEvictionRunsMillis 在空闲连接回收器线程运行期间休眠时间(毫秒), 该值不应该小于 1 秒,它决定线程多久验证空闲连接或丢弃连接的频率 5000(5秒) minEvictableIdleTimeMillis 连接在池中保持空闲而不被回收的最小时间(毫秒) 60000(60秒) removeAbandoned 标记是否删除泄露的连接,如果连接超出removeAbandonedTimeout的限制,且该属性设置为 true,则连接被认为是被泄露并且可以被删除 false removeAbandonedTimeout 泄露的连接可以被删除的超时时间(秒),该值应设置为应用程序查询可能执行的最长时间

# src/main/resources/application.properties


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring.datasource.url=jdbc:mysql://127.0.0.1/spring_boot_testing_storage
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.default-auto-commit=true
spring.datasource.tomcat.initial-size=3
spring.datasource.tomcat.max-active=120
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.test-while-idle=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-query-timeout=3
spring.datasource.tomcat.time-between-eviction-runs-millis=10000
spring.datasource.tomcat.min-evictable-idle-time-millis=120000
spring.datasource.tomcat.remove-abandoned=true
spring.datasource.tomcat.remove-abandoned-timeout=120

# src/main/resources/application.yml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
datasource:
url: jdbc:mysql://127.0.0.1/spring_boot_testing_storage
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
tomcat:
default-auto-commit: true
initial-size: 30
max-active: 120
max-wait: 10000
test-on-borrow: true
test-while-idle: true
validation-query: 'SELECT 1'
validation-query-timeout: 3
time-between-eviction-runs-millis: 10000
min-evictable-idle-time-millis: 120000
remove-abandoned: true
remove-abandoned-timeout: 120

Spring Boot Data Jpa 依赖声明:

# pom.xml


1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

HikariCP 连接池

Spring Boot 如果发现 Tomcat 连接池不可用,则尝试选择 HikariCP 作为默认连接池。HikariCP 连接池常用的属性:

idleTimeout 空闲超时时间(毫秒),只有在minimumIdle<maximumPoolSize时生效,超时的连接可能被回收,数值 0 表示空闲连接永不从池中删除 600000(10分钟) maxLifetime 连接池中的连接的最长生命周期(毫秒)。数值 0 表示不限制 1800000(30分钟) connectionTestQuery 连接池每分配一条连接前执行的查询语句(如:SELECT 1),以验证该连接是否是有效的。如果你的驱动程序支持 JDBC4,HikariCP 强烈建议我们不要设置此属性 minimumIdle 最小空闲连接数,HikariCP 建议我们不要设置此值,而是充当固定大小的连接池 与maximumPoolSize数值相同 maximumPoolSize 连接池中可同时连接的最大连接数,当池中没有空闲连接可用时,就会阻塞直到超出connectionTimeout设定的数值 poolName 连接池名称,主要用于显示在日志记录和 JMX 管理控制台中 auto-generated

# src/main/resources/application.properties


1
2
3
4
5
6
7
spring.datasource.url=jdbc:mysql://127.0.0.1/spring_boot_testing_storage
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.maximum-pool-size=150

# src/main/resources/application.yml


1
2
3
4
5
6
7
8
9
10
spring:
datasource:
url: jdbc:mysql://127.0.0.1/spring_boot_testing_storage
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
hikari:
auto-commit: true
connection-test-query: 'SELECT 1'
maximum-pool-size: 150

Spring Boot Data Jpa 依赖声明:

# pom.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.1</version>
</dependency>

DBCP 连接池

Spring Boot 如果发现 HikariCP 连接池不可用,则尝试选择 DBCP 作为默认连接池。DBCP(2) 连接池常用的属性:

removeAbandonedOnBorrow 标记是否删除泄露的连接,如果连接超出removeAbandonedTimeout的限制,且该属性设置为 true,则连接被认为是被泄露并且可以被删除 false removeAbandonedTimeout 泄露的连接可以被删除的超时时间(秒),该值应设置为应用程序查询可能执行的最长时间 300(5分钟) poolPreparedStatements 设置该连接池的预处理语句池是否生效 false

# src/main/resources/application.properties


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
spring.jmx.enabled=false
spring.datasource.url=jdbc:mysql://127.0.0.1/spring_boot_testing_storage
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dbcp2.default-auto-commit=true
spring.datasource.dbcp2.initial-size=30
spring.datasource.dbcp2.max-total=120
spring.datasource.dbcp2.max-idle=120
spring.datasource.dbcp2.min-idle=30
spring.datasource.dbcp2.max-wait-millis=10000
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.validation-query-timeout=3
spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.time-between-eviction-runs-millis=10000
spring.datasource.dbcp2.num-tests-per-eviction-run=10
spring.datasource.dbcp2.min-evictable-idle-time-millis=120000
spring.datasource.dbcp2.remove-abandoned-on-borrow=true
spring.datasource.dbcp2.remove-abandoned-timeout=120
spring.datasource.dbcp2.pool-prepared-statements=true

# src/main/resources/application.yml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
spring:
jmx:
enabled: false
datasource:
url: jdbc:mysql://127.0.0.1/spring_boot_testing_storage
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
dbcp2:
default-auto-commit: true
initial-size: 30
max-total: 120
max-idle: 120
min-idle: 30
max-wait-millis: 10000
validation-query: 'SELECT 1'
validation-query-timeout: 3
test-on-borrow: true
test-while-idle: true
time-between-eviction-runs-millis: 10000
num-tests-per-eviction-run: 10
min-evictable-idle-time-millis: 120000
remove-abandoned-on-borrow: true
remove-abandoned-timeout: 120
pool-prepared-statements: true

 
推荐文章