-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple DataSource configuration #234
Comments
The problem is that each The annotation creates a common data source through which you can initialize the database and create the required schemas. However, the other data sources must be created manually, according to your needs. These data sources actually refer to the same database, but with different connection parameters. Since the target database and the data source parameters may change during the tests (to achieve the desired test isolation), the best approach is to create a data source wrapper that applies these parameters at the moment of creating a new connection. You can either extend @RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureEmbeddedDatabase(beanName = "commonDataSource")
public class DatabaseWithMultipleSchemasTest {
@TestConfiguration // or @Configuration
static class Config {
@Bean
@Primary
public DataSource xDataSource(DataSource commonDataSource) {
UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
adapter.setTargetDataSource(commonDataSource);
adapter.setSchema("schemaX");
return adapter;
}
@Bean
public DataSource yDataSource(DataSource commonDataSource) {
UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
adapter.setTargetDataSource(commonDataSource);
adapter.setSchema("schemaY");
return adapter;
}
@Bean
public DataSource zDataSource(DataSource commonDataSource) {
UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
adapter.setTargetDataSource(commonDataSource);
adapter.setSchema("schemaZ");
return adapter;
}
}
@Autowired
@Qualifier("xDataSource")
private DataSource xDataSource;
@Autowired
@Qualifier("yDataSource")
private DataSource yDataSource;
@Autowired
@Qualifier("zDataSource")
private DataSource zDataSource;
// class body...
} |
In the future, I will try to come up with some easier way to handle these cases. |
@tomix26 thank you for your quick replay. I let you know if your solution works for me. I didn't try it yet |
Hello @tomix26 , Your solution with the Thanks again |
I have multiple datasources, each points to a different schema in the same database.
I'm using the AutoConfigureEmbeddedDatabase annotation like so:
So far, so good
Beside, I have a sql query which is joining multiple schema together; something like :
select * from x.table x join y.table y on ...
EmbeddedDatabase
will replace the original datasources whithout taking the original configuration (like the schema name).From the documentation I've found that I can set currentSchema with some additional configuration :
zonky.test.database.postgres.client.properties.currentSchema=x
But his configuration will affect all the replaced dataSources. I'm I right ?
Is there a way to use the same configuration with the datasource bean name, for instance:
zonky.test.database.postgres.client.xDataSource.properties.currentSchema=x
The text was updated successfully, but these errors were encountered: