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 |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java多线程编程基石ThreadPoolExecutor示例详解
这篇文章主要为大家介绍了Java多线程编程基石ThreadPoolExecutor示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-04-04Java中不得不知的Collection接口与Iterator迭代器
这篇文章主要介绍了Java中的Collection接口与Iterator迭代器,文中有详细的代码示例供大家参考,对我们的学习或工作有一定的帮助,需要的朋友可以参考下2023-06-06SpringBoot将Spring fox更换为Springdoc的方法详解
由于项目中使用Spring fox已经不维护更新了,代码扫描,扫出问题,需要将Spring fox更换为Spring Doc,所以本文给大家介绍了SpringBoot将Spring fox更换为Springdoc的方法,文中有相关的代码供大家参考,需要的朋友可以参考下2024-01-01
最新评论