Glasnhost
Glasnhost

Reputation: 1135

spring profile not loading some properties?

I would like two profiles, one with MySQL data source, and the other with H2 data source. So I've created

 application.properties:

 spring.jpa.hibernate.ddl-auto=create-drop
 spring.jpa.show-sql=true
 spring.datasource.url=jdbc:mysql://localhost:3306/boilerplate_springboot_restapi
 spring.datasource.username=root
 spring.datasource.password=password
 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

and

 application-test.properties:

 spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
 spring.h2.console.enabled=true
 spring.datasource.url=jdbc:h2:mem:testdb
 spring.datasource.driverClassName=org.h2.Driver
 spring.datasource.username=admin
 spring.datasource.password=admin

And I use the option -Dspring.profiles.active=test to run the application with the test profile.

The funny thing is that the test profile seems to pick up the H2 data source, but not the dialect, so it crashes. If I put the H2 config in application.properties, it works just fine.

Here the log of running the test profile:

[  restartedMain] c.b.r.SpringBoot2RestServiceApplication  : The following profiles are active: test
...
[  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
[         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
[         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.17.Final
[  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
[         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
...
Hibernate: drop table if exists book
Hibernate: drop table if exists hibernate_sequence
Hibernate: create table book....
[         task-1] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "create table book

I believe that the wrong hibernate dialect is the root cause of the crash. But I don't understand what I'm doing wrong.

UPDATE: If I set H2Dialect in application.properties, it picks it up correctly... it's like the setting in main file has priority over the setting in profile property file...

UPDATE: I don't have any @Profile in code nor in pom.xml

UPDATE: I created two properties files, one for MySQL and one for h2 and move all properties there. Now it seems to work.

I've found a similar issue with this Spring MVC application.properties not override by profile file application-dev.properties

Upvotes: 0

Views: 789

Answers (1)

M. Deinum
M. Deinum

Reputation: 124601

Your production configuration uses

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

whereas your test configuration uses

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

As both exists the first one will win (as the properties object is set as last). So use the same names in both files.

Upvotes: 2

Related Questions