8

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

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
SparX
  • 271
  • 2
  • 6
  • 15

2 Answers2

17

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

Community
  • 1
  • 1
jny
  • 8,007
  • 3
  • 37
  • 56
3

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.

Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119