Failed to validate a newly established connection

2019-06-16 18:31发布

"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.

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

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 MainApplication class:

@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.

How can I solve this problem?

2条回答
Melony?
2楼-- · 2019-06-16 18:52

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

See this answer for suggested validation queries for different databases

查看更多
叼着烟拽天下
3楼-- · 2019-06-16 19:01

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.

查看更多
登录 后发表回答