0

I have two different mysql database from the same connection and would like to use both in my application.properties like this way:

spring.datasource.url=jdbc:mysql://localhost:3306/membership
spring.datasource.username=root
spring.datasource.password=

spring.datasource.url=jdbc:mysql://localhost:3306/finance
spring.datasource.username=root
spring.datasource.password=

But this is not allowed (duplicate key's). I have found this guide but this is to much code i think not a elegant solution, looks like workaround for simple problem. Are there better and much simpler solutions?

PS: I am new to spring boot.

bedrin
  • 4,458
  • 32
  • 53
Rep
  • 109
  • 4
  • 15

2 Answers2

1

You have to decleare different keys for different datasource. The main config for multi datasource is(this is the demo):

@Bean
@Primary
@ConfigurationProperties("app.datasource.foo")
public DataSourceProperties fooDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("app.datasource.foo")
public DataSource fooDataSource() {
    return fooDataSourceProperties().initializeDataSourceBuilder().build();
}

@Bean
@ConfigurationProperties("app.datasource.bar")
public DataSourceProperties barDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@ConfigurationProperties("app.datasource.bar")
public DataSource barDataSource() {
    return barDataSourceProperties().initializeDataSourceBuilder().build();
}

You can refer to this link and the post to have an overview for the configuration.

Dave Pateral
  • 1,415
  • 1
  • 14
  • 21
-2

I think you can use only 1 MySQL database. Try to combine with PostgreSQL. Example

Cesar Wahyu
  • 117
  • 1
  • 1
  • 12