-
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
Problem with two data sources #236
Comments
I've already explained it here in detail. Below is the solution for your specific case. @RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureEmbeddedDatabase(beanName = "readWriteDataSource")
public class MultipleDataSourcesTest {
@TestConfiguration // or @Configuration
static class Config {
@Bean
public DataSource readOnlyDataSource(DataSource readWriteDataSource) {
ReadOnlyDataSourceAdapter adapter = new ReadOnlyDataSourceAdapter();
adapter.setTargetDataSource(readWriteDataSource);
return adapter;
}
}
public static class ReadOnlyDataSourceAdapter extends DelegatingDataSource {
@Override
public Connection getConnection() throws SQLException {
Connection connection = super.getConnection();
connection.setReadOnly(true);
return connection;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
Connection connection = super.getConnection(username, password);
connection.setReadOnly(true);
return connection;
}
}
@Autowired
@Qualifier("readWriteDataSource")
private DataSource readWriteDataSource;
@Autowired
@Qualifier("readOnlyDataSource")
private DataSource readOnlyDataSource;
// class body...
} |
Thank you! Is this indeed the right way to do this:
in the test:
|
Yes, that certainly looks functional. However, you don't actually use this library at all anymore, because the container you are starting this way is out of the control of this library. Which results in |
Will it still pull the container? |
As I wrote, it will work fine. You just don't use this library and you will lose the benefits it brings.
The library automatically replaces the original data sources with the testing ones. In general, this should not be a problem, because in most cases the app code refers to the |
Thank you for the explanation! |
I'm closing this issue is favor of #234, which seems to be similar. |
I have two data sources configured for my test like so:
When I execute tests, the queries against
readWriteDataSource
are executing fine but fail with "relation xyz does not exist" errors on thereadOnlyDataSource
. What could be the problem here?The text was updated successfully, but these errors were encountered: