SpringBoot2.0.3打印默認(rèn)數(shù)據(jù)源為 HikariDataSource (null)問題
SpringBoot2.0.3打印默認(rèn)數(shù)據(jù)源為 HikariDataSource (null)
剛剛開始以為DataSource是空對象,后來打印了下面的語句,才知道DataSource不是空的,我砸,我就好奇為什么 打印出HikariDataSource (null) 這樣的語句,真的坑。
@Autowired
DataSource dataSource;
@Autowired
DataSourceProperties dataSourceProperties;
@Test
public void contextLoads() throws SQLException {
System.out.println(String.format("數(shù)據(jù)源配置類:用戶名:%s,"
+"密碼:%s,資源定位符:%s,驅(qū)動:%s"
,dataSourceProperties.getUsername(),
dataSourceProperties.getPassword(),
dataSourceProperties.getUrl(),
dataSourceProperties.getDriverClassName()));
System.out.println(dataSource == null);//結(jié)果為:false
System.out.println("得到的數(shù)據(jù)源:"+dataSource);
System.out.println("得到的連接:"+dataSource.getConnection());
}
打印結(jié)果
得到的數(shù)據(jù)源: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默認(rèn)配置的默認(rèn)值如下
| name | 構(gòu)造器默認(rèn)值 | 默認(rèn)配置validate之后的值 | validate重置 |
|---|---|---|---|
| minIdle | -1 | 10 | minIdle<0或者minIdle>maxPoolSize,則被重置為maxPoolSize |
| maxPoolSize | -1 | 10 | 如果maxPoolSize小于1,則會被重置。當(dāng)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轉(zhuǎn)為秒數(shù)取整 與 1 取最大者 |
| idleTimeout | MINUTES.toMillis(10) = 600000 | 600000 | 如果idleTimeout+1秒>maxLifetime 且 maxLifetime>0,則會被重置為0;如果idleTimeout!=0且小于10秒,則會被重置為10秒 |
| leakDetectionThreshold | 0 | 0 | 如果大于0且不是單元測試,則進(jìn)一步判斷:(leakDetectionThreshold < SECONDS.toMillis(2) or (leakDetectionThreshold > maxLifetime && maxLifetime > 0),會被重置為0 . 即如果要生效則必須>0,而且不能小于2秒,而且當(dāng)maxLifetime > 0時不能大于maxLifetime |
| initializationFailTimeout | 1 | 1 | - |
| isAutoCommit | true | true | - |
| isReadOnly | false | fasle | - |
| isAllowPoolSuspension | false | false | - |
| isIsolateInternalQueries | false | false | - |
| isRegisterMbeans | false | false | - |
| sealed | false | true | 運(yùn)行啟動后這個標(biāo)志為true,表示不再運(yùn)行修改 |
| 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屬性:用來驗(yàn)證數(shù)據(jù)庫連接的語句,這個語句至少是返回一條數(shù)據(jù)的查詢語句。每種數(shù)據(jù)庫都有自己的驗(yàn)證語句。以下是不同數(shù)據(jù)庫對應(yīng)的驗(yàn)證語句: | ||
validation-query配置數(shù)據(jù)庫時,屬性validationQuery默認(rèn)值為“select 1”,對于oracle值應(yīng)為“select 1 from dual”
validationQuery屬性:用來驗(yàn)證數(shù)據(jù)庫連接的語句,這個語句至少是返回一條數(shù)據(jù)的查詢語句。每種數(shù)據(jù)庫都有自己的驗(yàn)證語句。
以下是不同數(shù)據(jù)庫對應(yīng)的驗(yàn)證語句:
| 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 |
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個組件,在整個生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
java統(tǒng)計(jì)字符串中指定元素出現(xiàn)次數(shù)方法
這篇文章主要介紹了java統(tǒng)計(jì)字符串中指定元素出現(xiàn)次數(shù)方法,需要的朋友可以參考下2015-12-12
通過java記錄數(shù)據(jù)持續(xù)變化時間代碼解析
這篇文章主要介紹了通過java記錄數(shù)據(jù)持續(xù)變化時間代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Spring?Boot?整合?FreeMarker?實(shí)例分享
這篇文章主要分享了Spring?Boot整合FreeMarker?實(shí)例FreeMarker是一款模板引擎,即一種基于模板和要改變的數(shù)據(jù),并用來生成輸出文本,更多相關(guān)介紹需要的小伙伴可以參考下面文章內(nèi)容2022-05-05
java中CompleteFuture與Future的區(qū)別小結(jié)
本文主要介紹了java中CompleteFuture與Future的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
淺談Java讀寫注冊表的方式Preferences與jRegistry
這篇文章主要介紹了淺談Java讀寫注冊表的方式Preferences與jRegistry,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Java UrlRewriter偽靜態(tài)技術(shù)運(yùn)用深入分析
通常我們?yōu)榱烁玫木徑夥?wù)器壓力,和增強(qiáng)搜索引擎的友好面,都將文章內(nèi)容生成靜態(tài)頁面,這就產(chǎn)生了偽靜態(tài)技術(shù),也就是我們常說的Url Rewriter重寫技術(shù)2012-12-12

