0

I am developling a Springboot Application with an active database. First, I tried configuring my database by adding the following in application.properties:

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/schema_name?characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hdm2ddl.auto = update

spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true

The Springboot Application can run perfectly fine with this configuration. However, as I try to move the above configuration lines in application-dev.properties and add spring.profiles.active = dev in my original application.properties, the following error yields:

2021-04-10 11:29:59.097  WARN 58148 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via 
factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2021-04-10 11:29:59.103  INFO 58148 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-04-10 11:29:59.134  INFO 58148 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-10 11:29:59.172 ERROR 58148 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active).

I searched for solutions but all of them suggests to either disable/exclude JPA (not the case) or configure the database connection properly (which I did, and succeeded in the main profile); from the error message it is also shown that dev profile is active. How can I solve this problem?

Power_tile
  • 578
  • 1
  • 6
  • 18
  • Did you change anything other than the application properties file? – code_mechanic Apr 10 '21 at 03:45
  • @code_mechanic Nothing. I tried moving the sql configurations back to ```application.properties```, and found that the problem still exists. However deleting ```spring.profiles.active = dev``` solves the problem. When ```spring.profiles.active = dev``` is indicated, even when ```application-dev.properties``` is empty, the same error will be thrown. – Power_tile Apr 10 '21 at 04:59
  • This profile thing works every time unless something else is changed or you manually try to do which spring boot does for you. – code_mechanic Apr 10 '21 at 05:01
  • @code_mechanic Is this probably because of the sql configuration can only be in the main profile? I tried putting ```spring.jpa.show-sql = true spring.jpa.properties.hibernate.format_sql = true``` into the ```application-dev.properties``` file and it worked, but putting the url, user, password, etc. information into the dev profile again yields the error. – Power_tile Apr 10 '21 at 06:01
  • It should not be the case, profile are used for the same purpose otherwise how would you connect to different datasources(except env variables) ? You might have messed up something with your configuration. If you can post minimum code here, someone might be able to help you, with current info, it hard to say what's wrong. – code_mechanic Apr 10 '21 at 06:05
  • Did you `clean` your project after changing the properties? Do you see `dev` as the active profile in the logging on startup? Maybe use `actuator` to detect which properties are loaded and from which file see [java-spring-hibernate-ddl-auto-none-property-is-somehow-overrided](https://stackoverflow.com/questions/67008255/java-spring-hibernate-ddl-auto-none-property-is-somehow-overrided) – Dirk Deyne Apr 10 '21 at 08:22
  • ??? @DirkDeyne I am using Spring Initializr + Vscode to create the springboot project, and after I run the ```Java: Clean Java Language Server Workspace``` and restarted my IDE it worked! Thanks – Power_tile Apr 10 '21 at 14:17
  • Furthermore, can I ask why does changing the properties need ```clean```ing? I did restart my project after changing the properties. – Power_tile Apr 10 '21 at 14:18

0 Answers0