如何使用springboot不自动初始化数据库连接池

发布时间:2021-09-10 13:20:57 作者:小新
来源:亿速云 阅读:257

小编给大家分享一下如何使用springboot不自动初始化数据库连接池,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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-8
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver

#连接池的配置信息
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.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;
    }

启动项目

如何使用springboot不自动初始化数据库连接池

我们可以发现,在没有配置spring.datasource.type时,spring boot默认的连接池是tomcat-jdbc

也就是说我们在application.properties中配置的连接池参数是无效的。

好,那我们再配置下这个属性,使用阿里巴巴的druid

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

再启动下

如何使用springboot不自动初始化数据库连接池

再来看看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" };

以上是“如何使用springboot不自动初始化数据库连接池”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 如何自动配置springboot
  2. 使用springboot 怎么通过代码自动生成pid

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot 数据库

上一篇:C++如何封装DLL供C#调用

下一篇:怎么通过重启路由的方法切换IP地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》