报错
ERROR 20760 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : testWhileIdle is true, validationQuery not set
完整日志:
2020-05-03 04:02:47.289 INFO 20760 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on jerryjin-MI with PID 20760 (E:\demo\target\classes started by jerryjin in E:\demo)
2020-05-03 04:02:47.292 INFO 20760 --- [ restartedMain] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-05-03 04:02:47.346 INFO 20760 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-05-03 04:02:47.346 INFO 20760 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-05-03 04:02:48.278 INFO 20760 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2020-05-03 04:02:48.285 INFO 20760 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-03 04:02:48.285 INFO 20760 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-03 04:02:48.357 INFO 20760 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-03 04:02:48.357 INFO 20760 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1011 ms
2020-05-03 04:02:48.369 INFO 20760 --- [ restartedMain] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2020-05-03 04:02:48.436 ERROR 20760 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : testWhileIdle is true, validationQuery not set
2020-05-03 04:02:48.442 INFO 20760 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2020-05-03 04:02:48.589 INFO 20760 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:file:E:/database/h2/test'
2020-05-03 04:02:48.642 INFO 20760 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2020-05-03 04:02:48.958 INFO 20760 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-03 04:02:49.123 INFO 20760 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path ''
2020-05-03 04:02:49.126 INFO 20760 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 2.21 seconds (JVM running for 3.216)
原因
不知道哪里的配置打开了它:test-while-idle: true
它打开了validation-query
就必须设置
解决方案
# Spring配置
spring:
datasource:
druid:
# 默认 true 为 true 时 validationQuery 必须设为非空字符串
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-on-borrow: false
# 默认 false 为 true 时 validationQuery 必须设为非空字符串
# 【建议】配置为true,不影响性能,并且保证安全性。申请连接的时候检测,
# 如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
test-while-idle: true
# 默认 true 为 true 时 validationQuery 必须设为非空字符串
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-on-return: false
# 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,
# testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
validation-query: select 1