SpringBoot2.0.3打印默認數(shù)據(jù)源為 HikariDataSource (null)問題
SpringBoot2.0.3打印默認數(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,驅動:%s"
,dataSourceProperties.getUsername(),
dataSourceProperties.getPassword(),
dataSourceProperties.getUrl(),
dataSourceProperties.getDriverClassName()));
System.out.println(dataSource == null);//結果為:false
System.out.println("得到的數(shù)據(jù)源:"+dataSource);
System.out.println("得到的連接:"+dataSource.getConnection());
}
打印結果
得到的數(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默認配置的默認值如下
| 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轉為秒數(shù)取整 與 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屬性:用來驗證數(shù)據(jù)庫連接的語句,這個語句至少是返回一條數(shù)據(jù)的查詢語句。每種數(shù)據(jù)庫都有自己的驗證語句。以下是不同數(shù)據(jù)庫對應的驗證語句: | ||
validation-query配置數(shù)據(jù)庫時,屬性validationQuery默認值為“select 1”,對于oracle值應為“select 1 from dual”
validationQuery屬性:用來驗證數(shù)據(jù)庫連接的語句,這個語句至少是返回一條數(shù)據(jù)的查詢語句。每種數(shù)據(jù)庫都有自己的驗證語句。
以下是不同數(shù)據(jù)庫對應的驗證語句:
| 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
JAVA生成八位不重復隨機數(shù)最快的方法總結(省時間省空間)
隨機數(shù)在實際中使用很廣泛,比如要隨即生成一個固定長度的字符串、數(shù)字,這篇文章主要給大家介紹了關于JAVA生成八位不重復隨機數(shù)最快的方法,文中介紹的方法省時間省空間,需要的朋友可以參考下2024-03-03
Mybatis-plus3.4.3下使用lambdaQuery報錯解決
最近在使用lambdaQuery().eq(CommonUser::getOpenId, openId).one()進行查詢報錯,本文主要介紹了Mybatis-plus3.4.3下使用lambdaQuery報錯解決,具有一定的參考價值,感興趣的可以了解一下2024-07-07
Java實現(xiàn)md5和base64加密解密的示例代碼
這篇文章主要介紹了Java實現(xiàn)md5和base64加密解密的示例代碼,幫助大家更好的利用Java加密解密文件,感興趣的朋友可以了解下2020-09-09
Java Date類常用示例_動力節(jié)點Java學院整理
在JDK1.0中,Date類是唯一的一個代表時間的類,但是由于Date類不便于實現(xiàn)國際化,所以從JDK1.1版本開始,推薦使用Calendar類進行時間和日期處理。這里簡單介紹一下Date類的使用,需要的朋友可以參考下2017-05-05

