溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用springboot不自動初始化數(shù)據(jù)庫連接池

發(fā)布時間:2021-09-10 13:20:57 來源:億速云 閱讀:283 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下如何使用springboot不自動初始化數(shù)據(jù)庫連接池,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

springboot不自動初始化數(shù)據(jù)庫連接池

簡介

有時候我們想自己動態(tài)的初始化數(shù)據(jù)庫連接池,但是springboot 的@SpringBootApplication注解會自動去初始化數(shù)據(jù)庫連接池,不配置的話會啟動失敗,如下提示

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})

就可以跳過數(shù)據(jù)庫的自動初始化,自己為所欲為了~

記錄下spring boot關(guān)于數(shù)據(jù)庫連接池的一個小坑

環(huán)境:spring boot 1.5、JDK1.8

application.properties配置

# 驅(qū)動配置信息
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不自動初始化數(shù)據(jù)庫連接池

我們可以發(fā)現(xiàn),在沒有配置spring.datasource.type時,spring boot默認的連接池是tomcat-jdbc

也就是說我們在application.properties中配置的連接池參數(shù)是無效的。

好,那我們再配置下這個屬性,使用阿里巴巴的druid

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

再啟動下

如何使用springboot不自動初始化數(shù)據(jù)庫連接池

再來看看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

以上是“如何使用springboot不自動初始化數(shù)據(jù)庫連接池”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI