SpringBoot4.5.2 整合HikariCP 数据库连接池操作

网友投稿 341 2022-12-12

SpringBoot4.5.2 整合HikariCP 数据库连接池操作

目录SpringBoot4.5.2 整合HikariCP 数据库连接池引入application.yaml输出HikariCP连接池及其在springboot中的配置主要配置如下

SpringBoot4.5.2 整合HikariCP 数据库连接池

Spring Boot 2.+默认使用的就是连接池HikariCP

所以,只要引入相关包即可

引入

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-jpa

mysql

mysql-connector-java

8.0.26

application.yaml

spring:

########-spring datasource-########

datasource:

#账号配置

url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8

username: root

password: root

#hikari数据库连接池

hikari:

pool-name: Retail_HikariCP

minimum-idle: 5 #最小空闲连接数量

idle-timeout: 180000 #空闲连接存活最大时间,默认600000(10分钟)

maximum-pool-size: 10 #连接池最大连接数,默认是10

auto-commit: true #此属性控制从池返回的连接的默认自动提交行为,默认值:true

max-lifetime: 1800000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟

connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000

connection-test-query: SELECT 1

输出

. ____ _ __ _ _

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v2.5.2)

2021-08-09 14:25:51.322 INFO 1298 --- [ main] com.example.demorsa.DemoRsaApplication : Starting DemoRsaApplication using Java 11.0.2 on foxdeiMac with PID 1298

2021-08-09 14:25:51.324 INFO 1298 --- [ main] com.example.demorsa.DemoRsaApplication : No active profile set, falling back to default profiles: default

2021-08-09 14:25:51.905 INFO 1298 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2021-08-09 14:25:51.921 INFO 1298 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.

2021-08-09 14:25:52.272 INFO 1298 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)

2021-08-09 14:25:52.279 INFO 1298 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

2021-08-09 14:25:52.279 INFO 1298 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]

2021-08-09 14:25:52.350 INFO 1298 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2021-08-09 14:25:52.350 INFO 1298 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 954 ms

2021-08-09 14:25:52.456 INFO 1298 --- [ main] com.zaxxer.hikari.HikariDataSource : Retail_HikariCP - Starting...

2021-08-09 14:25:52.600 INFO 1298 --- [ main] com.zaxxer.hikari.HikariDataSource : Retail_HikariCP - Start completed.

2021-08-09 14:25:52.633 INFO 1298 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]

2021-08-09 14:25:52.661 INFO 1298 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final

2021-08-09 14:25:52.740 INFO 1298 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}

2021-08-09 14:25:52.814 INFO 1298 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect

2021-08-09 14:25:52.919 INFO 1298 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]

2021-08-09 14:25:52.927 INFO 1298 --- [ main] j.LocalContainerEntityXAULvYrManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2021-08-09 14:25:52.958 WARN 1298 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

2021-08-09 14:25:53.236 INFO 1298 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''

2XAULvYr021-08-09 14:25:53.245 INFO 1298 --- [ main] com.example.demorsa.DemoRsaApplication : Started DemoRsaApplication in 2.233 seconds (JVM running for 2.8)

HikariCP连接池及其在springboot中的配置

主要配置如下

配置项

描述

构造器默认值

默认配置validate之后的值

validate重置

autoCommit

自动提交从池中返回的连接

true

true

-

connectionTimeout

等待来自池的连接的最大毫秒数

SECONDS.toMillis(30) = 30000

30000

如果小于250毫秒,则被重置回30秒

idleTimeout

连接允许在池中闲置的最长时间

MINUTES.toMillis(10) = 600000

600000

如果idleTimeout+1秒>maxLifetime 且 maxLifetime>0,则会被重置为0(代表永远不会退出);如果idleTimeout!=0且小于10秒,则会被重置为10秒

maxLifetime

池中连接最长生命周期

MINUTES.toMillis(30) = 1800000

1800000

如果不等于0且小于30秒则会被重置回30分钟

connectionTestQuery

如果您的驱动程序支持JDBC4,我们强烈建议您不要设置此属性

null

null

-

minimumIdle

池中维护的最小空闲连接数

-1

10

minIdle<0或者minIdle>maxPoolSize,则被重置为maxPoolSize

maximumPoolSize

池中最大连接数,包括闲置和使用中的连接

-1

10

如果maxPoolSize小于1,则会被重置。当minIdle<=0被重置为DEFAULT_POOL_SIZE则为10;如果minIdle>0则重置为minIdle的值

metricRegistry

该属性允许您指定一个 Codahale / Dropwizard MetricRegistry 的实例,供池使用以记录各种指标

null

null

-

healthCheckRegistry

该属性允许您指定池使用的Codahale / Dropwizard HealthCheckRegistry的实例来报告当前健康信息

null

null

-

poolName

连接池的用户定义名称,主要出现在日志记录和JMX管理控制台中以识别池和池配置

null

HikariPool-1

-

initializationFailTimeout

如果池无法成功初始化连接,则此属性控制池是否将 fail fast

1

1

-

isolateInternalQueries

是否在其自己的事务中隔离内部池查询,例如连接活动测试

false

false

-

allowPoolSuspension

控制池是否可以通过JMX暂停和恢复

false

false

-

readOnly

从池中获取的连接是否默认处于只读模式

false

false

-

registerMbeans

是否注册JMX管理Bean(MBeans)

false

false

-

catalog

为支持 catalog 概念的数据库设置默认 catalog

driver default

null

-

connectionInitSql

该属性设置一个SQL语句,在将每个新连接创建后,将其添加到池中之前执行该语句。

null

null

-

driverClassName

HikariCP将尝试通过仅基于jdbcUrl的DriverManager解析驱动程序,但对于一些较旧的驱动程序,还必须指定driverClassName

null

null

-

transactionIsolation

控制从池返回的连接的默认事务隔离级别

null

null

-

validationTimeout

连接将被测试活动的最大时间量

SECONDS.toMillis(5) = 5000

5000

如果小于250毫秒,则会被重置回5秒

leakDetectionThreshold

记录消息之前连接可能离开池的时间量,表示可能的连接泄漏

0

0

如果大于0且不是单元测试,则进一步判断:(leakDetectionThreshold < SECONDS.toMillis(2) or (leakDetectionThreshold > maxLifetime && maxLifetime > 0),会被重置为0 . 即如果要生效则必须>0,而且不能小于2秒,而且当maxLifetime > 0时不能大于maxLifetime

dataSource

这个属性允许你直接设置数据源的实例被池包装,而不是让HikariCP通过反射来构造它

null

null

-

schema

该属性为支持模式概念的数据库设置默认模式

driver default

null

-

threadFactory

此属性允许您设置将用于创建池使用的所有线程的java.util.concurrent.ThreadFactory的实例。

null

null

-

scheduledExecutor

此属性允许您设置将用于各种内部计划任务的java.util.concurrent.ScheduledExecutorService实例

null

null

-

## 数据库配置

spring.datasource.type=com.zaxxer.hikari.HikariDaXAULvYrtaSource

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 连接池配置 ------ 详细配置请访问:https://github.com/brettwooldridge/HikariCP

## 最小空闲连接数量

spring.datasource.hikari.minimum-idle=5

## 空闲连接存活最大时间,默认600000(10分钟)

spring.datasource.hikari.idle-timeout=180000

## 连接池最大连接数,默认是10

spring.datasource.hikari.maximum-pool-size=10

## 此属性控制从池返回的连接的默认自动提交行为,默认值:true

spring.datasource.hikari.auto-commit=true

## 连接池母子

spring.datasource.hikari.pool-name=MyHikariCP

## 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟

spring.datasource.hikari.max-lifetime=1800000

## 数据库连接超时时间,默认30秒,即30000

spring.datasource.hikari.connection-timeout=30000

spring.datasource.hikari.connection-test-query=SELECT 1

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot环境下junit单元测试速度优化方式
下一篇:SpringBoot实战之高效使用枚举参数(原理篇)案例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~