SpringBoot 使用Hikaricp连接池

时间:2021-11-24 12:04:01

1.添加pom.xml依赖

如果是SpringBoot2.0,那么默认的连接池就是Hikaricp,不需要配置

其他的,如果继承

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>

直接添加Hikaricp包就可以

          <dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.1.</version>
</dependency>

反之使用jdbc包的要去除掉tomcat的连接配置

 <!-- JDBC连接数据库,因为要用HikariCP,所以需要将SpringBoot中的tomcat-jdbc排除 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- HikariCP 连接池依赖,从父依赖获取额版本 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>

2.添加配置:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=hongda$
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.max-idle=
spring.datasource.max-active=
spring.datasource.max-lifetime=
spring.datasource.log-abandoned=true
spring.datasource.remove-abandoned=true
spring.datasource.remove-abandoned-timeout=
spring.datasource.initialize=false
spring.datasource.sqlScriptEncoding=UTF-

DataSourceConfig:

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /**
* HikariCP连接池配置
*/
@Configuration
public class DataSourceConfig { @Value("${spring.datasource.url}")
private String dataSourceUrl; @Value("${spring.datasource.username}")
private String user; @Value("${spring.datasource.password}")
private String password; @Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(dataSourceUrl); //数据源
config.setUsername(user); //用户名
config.setPassword(password); //密码
config.addDataSourceProperty("cachePrepStmts", "true"); //是否自定义配置,为true时下面两个参数才生效
config.addDataSourceProperty("prepStmtCacheSize", ""); //连接池大小默认25,官方推荐250-500
config.addDataSourceProperty("prepStmtCacheSqlLimit", ""); //单条语句最大长度默认256,官方推荐2048
config.addDataSourceProperty("useServerPrepStmts", "true"); //新版本MySQL支持服务器端准备,开启能够得到显著性能提升
config.addDataSourceProperty("useLocalSessionState", "true");
config.addDataSourceProperty("useLocalTransactionState", "true");
config.addDataSourceProperty("rewriteBatchedStatements", "true");
config.addDataSourceProperty("cacheResultSetMetadata", "true");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("maintainTimeStats", "false"); HikariDataSource ds = new HikariDataSource(config);
return ds;
}
}

参考:

https://gitee.com/mirrors/hikaricp

http://fanlychie.github.io/post/spring-boot-tomcat-pool-hikaricp-dbcp-dbcp2.html

https://www.jianshu.com/p/f728e8c131a9

https://blog.csdn.net/chen15369337607/article/details/78142751

http://blog.didispace.com/Springboot-2-0-HikariCP-default-reason/

https://my.oschina.net/u/3452433/blog/1607850