SpringBoot 使用Hikaricp连接池
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
SpringBoot 使用Hikaricp连接池的更多相关文章
- SpringBoot Beans定义 连接池
SpringBoot Beans定义 原有Spring框架,定义Bean方法如下 xml配置 组件扫描.@Controller.@Service... 原有Spring框架,参数注入方法如下 常用的参 ...
- HikariCP连接池
1.HikariCP连接池是什么? HikariCP是数据库连接池,而且是号称史上最快的,而且目前来看确实是这样的,SpringBoot2.0也已经采用HikariCP作为默认连接池配置. githu ...
- Java代码生成器加入postgresql数据库、HikariCP连接池、swagger2支持!
目录 前言 PostgreSql VS MySql HikariCP VS Druid Swagger2 自定义参数配置一览 结语 前言 最近几天又抽时间给代码生成器增加了几个新功能(预计今晚发布 ...
- 在 Spring Boot 中使用 HikariCP 连接池
上次帮小王解决了如何在 Spring Boot 中使用 JDBC 连接 MySQL 后,我就一直在等,等他问我第三个问题,比如说如何在 Spring Boot 中使用 HikariCP 连接池.但我等 ...
- HikariCP连接池及其在springboot中的配置
主要配置如下: 配置项 描述 构造器默认值 默认配置validate之后的值 validate重置 autoCommit 自动提交从池中返回的连接 true true - connectionTime ...
- SpringBoot性能优化之HikariCP连接池
以前一直使用阿里Druid数据库连接池,这段时间听说有个号称速度最快.代码最简的后起之秀——HikariCP,于是动手实践一下 1.依赖如下: <?xml version="1.0&q ...
- Spring系列之HikariCP连接池
上两篇文章,我们讲到了Spring中如何配置单数据源和多数据源,配置数据源的时候,连接池有很多选择,在SpringBoot 1.0中使用的是Tomcat的DataSource,在SpringBoot ...
- springboot使用lettuce连接池
springboot对连接池的使用非常智能,配置文件中添加lettuce.pool相关配置,则会使用到lettuce连接池,并将相关配置设置为连接池相关参数,(前提是这些参数是springboot配置 ...
- spring引入HikariCP连接池
1.导入jar包 2.applicationContext.xml中配置 <bean id="dataSource" class="com.zaxxer.hikar ...
随机推荐
- linux命令目录
一.文件和目录.(文件目录的增删改查) ls pwd cd mkdir touch rmdir ln dd rm cp mv nl cat tac more less head tail stat # ...
- [Unit Test] Unit Test Brief Introduction
Levels of Testing- Acceptance- Performance- Functional- Integration- Unit Why Unit Testing- Feedback ...
- 如何运行ruby代码
第一种,ruby -e 在命令行中运行下面命令,-e的意思是,把后面的字符串当作脚本执行 ruby -e "print 'hello'" 使用irb交互控制台 在命令行输入irb ...
- strlen实现
1.strlen函数. 普通版实现方法, int strlen( char *s) { int length = 0; while(*s++) length++; return length; } 优 ...
- kali linux android木马(内网+外网)
1,内网木马 先生成一个小马 msfpayload -p android/meterpreter/reverse_tcp LHOST=192.168.8.104 LPORT=5555 R > a ...
- JS中常用的输出方式(五种)
1.alert("要输出的内容"); ->在浏览器中弹出一个对话框,然后把要输出的内容展示出来 ->alert都是把要输出的内容首先转换为字符串然后在输出的 2.doc ...
- Spark学习之路 (六)Spark Transformation和Action
Transformation算子 基本的初始化 java static SparkConf conf = null; static JavaSparkContext sc = null; static ...
- python type的用法
目录 描述 语法 用法 type和isinstance Type和Object 描述 python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. ...
- restful的特点
1. 资源(Resources) REST的名称”表现层状态转化”中,省略了主语.”表现层”其实指的是”资源”(Resources)的”表现层”. 所谓”资源”,就是网络 ...
- Linux服务器---邮件服务openwebmail配置
配置openwebmail 通过修改配置文件openwebmail.conf,实现个性化邮箱定制.用户可以自由的更改邮箱logo.域名.容量 .签名等信息. 1.重定向index,快速打开邮箱.将下面 ...