postgresql 无法验证新建立的连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33100423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:04:19  来源:igfitidea点击:

Failed to validate a newly established connection

javaspringpostgresqlspring-bootspring-data-jpa

提问by SparX

"Failed to validate a newly established connection" error occurs.

出现“无法验证新建立的连接”错误。

I googled and read every question related to this error. But did not able to find solution.

我用谷歌搜索并阅读了与此错误相关的每个问题。但是没能找到解决办法。

I'm using spring-boot-starter-data-jpa.

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

It works without any errors with Postgresql. But I want to use embedded database!!!

它与 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 of MainApplicationclass:

MainApplication类的标题:

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

This error thrown only when I use embedded databases (at least Derby, HSQLDB) and not always. Sometimes it starts normally, finds and saves entities without errors, but sometimes occurs after waiting some period or immediately after successful transaction.

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

How can I solve this problem?

我怎么解决这个问题?

回答by jny

Your validation query is the problem. While SELECT 1without FROMworks in Postgres, it is not valid in hsqldb.

您的验证查询是问题所在。虽然在 中SELECT 1没有FROM作品Postgres,但它在 hsqldb 中无效。

See this answerfor suggested validation queries for different databases

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

回答by Robert Niestroj

Try it without the validation query. If the HSQLDB driver is JDBC4 compliant it should use and work with the Connection.isValid(int timeout)method.

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