SpringBoot2.0.3打印默認數據源為 HikariDataSource (null)問題

SpringBoot2.0.3打印默認數據源為 HikariDataSource (null)

剛剛開始以為DataSource是空對象,後來打印瞭下面的語句,才知道DataSource不是空的,我砸,我就好奇為什麼 打印出HikariDataSource (null) 這樣的語句,真的坑。

    @Autowired
    DataSource dataSource;
    @Autowired
    DataSourceProperties dataSourceProperties;
    @Test
    public void contextLoads() throws SQLException {
        System.out.println(String.format("數據源配置類:用戶名:%s,"
        +"密碼:%s,資源定位符:%s,驅動:%s"
                ,dataSourceProperties.getUsername(),
                dataSourceProperties.getPassword(),
                dataSourceProperties.getUrl(),
                dataSourceProperties.getDriverClassName()));
        System.out.println(dataSource == null);//結果為:false
        System.out.println("得到的數據源:"+dataSource);
        System.out.println("得到的連接:"+dataSource.getConnection());
    }

打印結果

得到的數據源:HikariDataSource (null) 2020-09-08 00:16:53.612 INFO 13316 — [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Starting… Tue Sep 08 00:16:53 CST 2020 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 2020-09-08 00:16:54.330 INFO 13316 — [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Start completed.

得到的連接:HikariProxyConnection@722513129 wrapping com.mysql.jdbc.JDBC4Connection@52169758 2020-09-08 00:16:54.335 INFO 13316 — [ Thread-2] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@5b799640: startup date [Tue Sep 08 00:16:51 CST 2020]; root of context hierarchy 2020-09-08 00:16:54.337 INFO 13316 — [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Shutdown initiated… 2020-09-08 00:16:54.339 INFO 13316 — [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Shutdown completed.

需要註意

SpringBoot2.0.3使用的Driver是com.mysql.jdbc.Driver

springboot的HikariDataSource默認配置的默認值如下

name 構造器默認值 默認配置validate之後的值 validate重置
minIdle -1 10 minIdle<0或者minIdle>maxPoolSize,則被重置為maxPoolSize
maxPoolSize -1 10 如果maxPoolSize小於1,則會被重置。當minIdle<=0被重置為DEFAULT_POOL_SIZE則為10;如果minIdle>0則重置為minIdle的值
maxLifetime MINUTES.toMillis(30) = 1800000 1800000 如果不等於0且小於30秒則會被重置回30分鐘
connectionTimeout SECONDS.toMillis(30) = 30000 30000 如果小於250毫秒,則被重置回30秒
validationTimeout SECONDS.toMillis(5) = 5000 5000 如果小於250毫秒,則會被重置回5秒
loginTimeout 10 30 Math.max(1, (int) MILLISECONDS.toSeconds(500L + connectionTimeout)),為connectionTimeout+500ms轉為秒數取整 與 1 取最大者
idleTimeout MINUTES.toMillis(10) = 600000 600000 如果idleTimeout+1秒>maxLifetime 且 maxLifetime>0,則會被重置為0;如果idleTimeout!=0且小於10秒,則會被重置為10秒
leakDetectionThreshold 0 0 如果大於0且不是單元測試,則進一步判斷:(leakDetectionThreshold < SECONDS.toMillis(2) or (leakDetectionThreshold > maxLifetime && maxLifetime > 0),會被重置為0 . 即如果要生效則必須>0,而且不能小於2秒,而且當maxLifetime > 0時不能大於maxLifetime
initializationFailTimeout 1 1
isAutoCommit true true
isReadOnly false fasle
isAllowPoolSuspension false false
isIsolateInternalQueries false false
isRegisterMbeans false false
sealed false true 運行啟動後這個標志為true,表示不再運行修改
poolName null HikariPool-1
catalog null null
connectionInitSql null null
connectionTestQuery null null
dataSourceClassName null null
schema null null
transactionIsolationName null null
dataSource null null
dataSourceProperties {} {}
threadFactory null null
scheduledExecutor null null
metricsTrackerFactory null null
metricRegistry null null
healthCheckRegistry null null
healthCheckProperties {} {}
validation-query validationQuery屬性:用來驗證數據庫連接的語句,這個語句至少是返回一條數據的查詢語句。每種數據庫都有自己的驗證語句。以下是不同數據庫對應的驗證語句:

validation-query配置數據庫時,屬性validationQuery默認值為“select 1”,對於oracle值應為“select 1 from dual”

validationQuery屬性:用來驗證數據庫連接的語句,這個語句至少是返回一條數據的查詢語句。每種數據庫都有自己的驗證語句。

以下是不同數據庫對應的驗證語句:

DataBase validationQuery
hsqldb select 1 from INFORMATION_SCHEMA.SYSTEM_USERS
Oracle select 1 from dual
DB2 select 1 from sysibm.sysdummy1
MySql select 1
Microsoft SqlServer select1
postgresql select version()
ingres select 1
derby values 1
H2 select 1

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

推薦閱讀: