Issue
I am trying to read and write into two databases and I am running into issues:
config.yml
database1:
driverClass: com.mysql.jdbc.Driver
user: user1
password: user!23
url: jdbc:mysql://url.to.connect:3306/db1
properties: charSet: UTF-8
maxWaitForConnection: 1s
minSize: 8
maxSize: 32
checkConnectionWhileIdle: false
checkConnectionHealthWhenIdleFor: 10s
closeConnectionIfIdleFor: 1 minute
database2:
driverClass: com.mysql.jdbc.Driver
user: user2
password: user!23
url: jdbc:mysql://url.to.connect:3306/db2
properties: charSet: UTF-8
maxWaitForConnection: 1s
minSize: 8
maxSize: 32
checkConnectionWhileIdle: false
checkConnectionHealthWhenIdleFor: 10s
closeConnectionIfIdleFor: 1 minute
configuration.java
public class ExampleConfiguration extends Configuration {
@Valid
@NotNull
private DataSourceFactory database1 = new DataSourceFactory();
@Valid
@NotNull
private DataSourceFactory database2 = new DataSourceFactory();
@JsonProperty("database1")
public DataSourceFactory getDb1DataSourceFactory() {
return database1;
}
@JsonProperty("database2")
public DataSourceFactory getDb2DataSourceFactory() {
return database2;
}
}
Now following: Maintain multiple migration files in parallel in dropwizard, I tried to add a bootstrap bundle as follows:
@Override
public void initialize(Bootstrap< ExampleConfiguration > bootstrap) {
bootstrap.addCommand(new RenderCommand());
bootstrap.addBundle(new AssetsBundle());
bootstrap.addBundle(new MigrationsBundle<ExampleConfiguration>() {
@Override
public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
return configuration.getDb1DataSourceFactory();
}
});
bootstrap.addBundle(new MigrationsBundle<ExampleConfiguration>() {
@Override
public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
return configuration.getDb2DataSourceFactory();
}
});
bootstrap.addBundle(hibernateBundle);
}
But I got command 'db' has been already used
. I am still unclear what is happening in initialize()
. How can I switch between DB instances for read and write operations?
Thank you.
Solution
You should override each MigrationsBundle
implementation's method name
to provide unique command's name. Then using CLI you will need to specify correct name to use according database configuration.
Answered By - Yurii Melnychuk