使用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;
    }

啟動項目

這裡寫圖片描述

我們可以發現,在沒有配置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

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: