I'm running h2 database with spring boot. Basic configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Everything runs smoothly.
Now I want to define another Datasource bean in my application
@Bean
@Qualifier("oracleDatasource")
public DataSource oracleDataSource(@Qualifier("oracleDBProperties") DataSourceProperties oracleDBProperties) {
return oracleDBProperties.initializeDataSourceBuilder().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.oracle.datasource")
public DataSourceProperties oracleDBProperties() {
return new DataSourceProperties();
}
spring.oracle.datasource.url=jdbc:oracle:thin:url:db
spring.oracle.datasource.username=test
spring.oracle.datasource.password=test
spring.oracle.datasource.driver-class-name=oracle.jdbc.OracleDriver
The oracle datasource initializes successfully but this time h2 configuration is ignored. Looks like defining another DataSource bean suppresses the spring's autoconfiguration for h2.
How can I avoid that suppression or is there a way to define h2 same way as I did for oracle?
I have tried to do the same type of bean config for h2 as I've done for oracle but it doesn't seem to work.
UPDATE
Fixed the problem with following
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.h2.datasource")
public DataSourceProperties h2DBProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@Qualifier("h2Datasource")
public DataSource h2DataSource(@Qualifier("h2DBProperties") DataSourceProperties h2DBProperties) {
return h2DBProperties.initializeDataSourceBuilder().build();
}
spring.h2.datasource.url=jdbc:h2:mem:testdb
spring.h2.datasource.driverClassName=org.h2.Driver
spring.h2.datasource.username=sa
spring.h2.datasource.password=
Adding the @Primary annotation didn't let anything suppress it. Although, still not sure to consider this as a right solution.