无法验证新建立的连接

Failed to validate a newly established connection

"Failed to validate a newly established connection" 发生错误。

我用谷歌搜索并阅读了与此错误相关的所有问题。但是没能找到解决方案。

我正在使用 spring-boot-starter-data-jpa

它在 Postgresql 上没有任何错误。但是我想用嵌入式数据库!!!

application.properties:

#We don't need JMX here - disabling it allows for faster startup
spring.jmx.enabled=false
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop

spring.datasource.driver-class-name=org.hsqldb.jdbcDriver
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.datasource.url=jdbc:hsqldb:file:${user.home}/db/data;user=sa;password=123;
spring.datasource.username=sa
spring.datasource.password=123

Header MainApplication class:

@ComponentScan(value = {"db", "app", "ui"})
@EnableJpaRepositories(basePackages = "db")
@EntityScan(basePackages = "db")
@EnableTransactionManagement
@SpringBootApplication

仅当我使用嵌入式数据库(至少是 Derby、HSQLDB)时才会抛出此错误,但并非总是如此。有时启动正常,找到并保存实体没有错误,但有时等待一段时间后或交易成功后立即出现。

我该如何解决这个问题?

您的验证查询有问题。虽然没有 FROMSELECT 1Postgres 中有效,但在 hsqldb 中无效。

请参阅此answer,了解针对不同数据库的建议验证查询

在没有验证查询的情况下试试。如果 HSQLDB 驱动程序是 JDBC4 兼容的,它应该使用 Connection.isValid(int timeout) 方法。