Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot : Cannot load driver class: org.hsqldb.jdbcDriver

I have a simple Spring Boot application (generated through Spring Roo).

The database is configured as following :

spring.datasource.driver-class-name=org.hsqldb.jdbcDriver
spring.datasource.url=jdbc\:hsqldb\:mem\:PetClinic
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.messages.encoding=ISO-8859-1
spring.messages.fallback-to-system-locale=false
spring.thymeleaf.mode=html

Here are the how I declared the HSQLDB dependency :

<dependency>
  <groupId>org.hsqldb</groupId>
  <artifactId>hsqldb</artifactId>
  <scope>provided</scope>
</dependency>

When I start the application, I get the error :

Caused by: java.lang.IllegalStateException: Cannot load driver class: org.hsqldb.jdbcDriver
    at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:214) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]

The Spring-boot-autoconfigure module tries to load the class with the ClassUtils utility class that loads the current context classes.

I wonder if this method works fine since I use a Tomcat container which is responsible to load the Maven dependencies ? Why even with the JAR in the libs directory Spring is not able to find it ?

like image 281
Marcel Avatar asked Aug 22 '17 22:08

Marcel


2 Answers

I see the scope you gave is provided, <scope>provided</scope>, I don't think Tomcat provides the hsqldb.jar with it out of the box.

So try removing the provided scope.

like image 173
Anil Kumar Athuluri Avatar answered Nov 16 '22 01:11

Anil Kumar Athuluri


  1. Remove <scope>provided</scope> from your pom.xml
  2. Remove both spring.datasource.driver-class-name and spring.datasource.url properties from your application properties

Because:

  • when spring.datasource.url is provided the driver class name is redundant as Spring Boot will automatically attempt to load the correct driver.
  • since you want to use an embedded database you don't need to provide spring.datasource.url at all. Just need to have an embedded database JAR on the classpath (like HSQLDB)

Relevant docs snippet:

Spring Boot can auto-configure embedded H2, HSQL and Derby databases. You don’t need to provide any connection URLs, simply include a build dependency to the embedded database that you want to use.

Please read the Working with SQL databases section in of Spring Boot documentation. Everything I said is mentioned there, so you can get more detail.

like image 43
Strelok Avatar answered Nov 16 '22 01:11

Strelok