spring works well when we use the default datasource and ways we can use in-build spring jpa.
So currently what we do is the following
specify the config for DB in the application.properties
myapp.datasource.url=jdbc:mysql:thin:@localhost:1521:myschema
myapp.datasource.username=user
myapp.datasource.password=password
myapp.datasource.driver-class=com.mysql.cj.jdbc.Driver
Custom datasource
@ConfigurationProperties(prefix = "myapp.datasource")
@Bean
public DataSource mySqlDataSource()
{
return DataSourceBuilder.create().build();
}
We have the same application running for multiple clients. Problem is each client has their own DB schema.
So, the problem now is that we need to be able to serve each client but in order to do this, we need to create multiple datasources for instance:
@ConfigurationProperties(prefix = "myapp.partner1.datasource")
@Bean
public DataSource mySqlDataSourcePartner1()
{
return DataSourceBuilder.create().build();
}
@ConfigurationProperties(prefix = "myapp.partner2.datasource")
@Bean
public DataSource mySqlDataSourcePartner2()
{
return DataSourceBuilder.create().build();
}
@ConfigurationProperties(prefix = "myapp.partner3.datasource")
@Bean
public DataSource mySqlDataSourcePartner3()
{
return DataSourceBuilder.create().build();
}
and so on...
Is there a generic and more efficient way of doing this? where if in future when a new partner is added we can just specify the config in application properties and get that working?