使用springboot不自动初始化数据库连接池
目录
- springboot不自动初始化数据库连接池
- 简介
- 解决方案
- 记录下spring boot关于数据库连接池的一个小坑
- application.properties配置
- 先找到这个类
- 在下面的源码中打个断点
- 启动项目
springboot不自动初始化数据库连接池
简介
有时候我们想自己动态的初始化数据库连接池,但是springboot 的@SpringBootApplication注解会自动去初始化数据库连接池,不配置的话会启动失败,如下提示
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
INFO - Unregistering JMX-exposed beans on shutdown
解决方案
办法就是排除自动初始化的类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})public class Application implements CommandLineRunner {...}
加上这么一句
(exclude = {DataSourceAutoConfiguration.class})
就可以跳过数据库的自动初始化,自己为所欲为了~
记录下spring boot关于数据库连接池的一个小坑 环境:spring boot 1.5、JDK1.8
application.properties配置
# 驱动配置信息spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mealsystem?useUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456spring.datasource.driverClassName = com.mysql.jdbc.Driver#连接池的配置信息spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20spring.datasource.maxWait=60000spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20spring.datasource.filters=stat,wall,log4jspring.datasource.connectionProperties=druid.stat.mergeSql=true; druid.stat.slowSqlMillis=5000
先找到这个类
org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
在下面的源码中打个断点
public DataSource build() {Class extends DataSource> type = this.getType(); DataSource result = (DataSource)BeanUtils.instantiate(type); this.maybeGetDriverClassName(); this.bind(result); return result; }
启动项目
文章图片
我们可以发现,在没有配置spring.datasource.type时,spring boot默认的连接池是tomcat-jdbc
也就是说我们在application.properties中配置的连接池参数是无效的。
好,那我们再配置下这个属性,使用阿里巴巴的druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
再启动下
文章图片
再来看看1.5版本org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder的源码
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {"org.apache.tomcat.jdbc.pool.DataSource","com.zaxxer.hikari.HikariDataSource","org.apache.commons.dbcp.BasicDataSource", // deprecated"org.apache.commons.dbcp2.BasicDataSource" };
- spring boot 1.5的版本默认连接池为tomcat-jdbc
- spring boot 2.0的版本默认连接池为HikariCP
推荐阅读
- 急于表达——往往欲速则不达
- 2018-02-06第三天|2018-02-06第三天 不能再了,反思到位就差改变
- 家乡的那条小河
- 一个人的碎碎念
- 赠己诗
- 这辈子我们都不要再联系了
- 死结。
- 我从来不做坏事
- 时间老了
- 我错了,余生不再打扰