Question

I've already gone through the existing questions on this but still not satisfied to the core.

To add multiple database configuration, following is the info i got so far is -

1) Update config.yml file -

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
  validationQuery: "/* MyService Health Check */ SELECT 1"
  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
  validationQuery: "/* MyService Health Check */ SELECT 1"
  minSize: 8
  maxSize: 32
  checkConnectionWhileIdle: false
  checkConnectionHealthWhenIdleFor: 10s
  closeConnectionIfIdleFor: 1 minute

2) Add the changes to java configuration file.

public class DropWizardConfiguration extends Configuration{

    @Valid
    @NotNull
    @JsonProperty
    private DatabaseConfiguration database1 = new DatabaseConfiguration();

    @Valid
    @NotNull
    @JsonProperty
    private DatabaseConfiguration database2 = new DatabaseConfiguration();

    public DatabaseConfiguration getDatabaseConfiguration1()
    {       
        return database1;
        }

        public DatabaseConfiguration getDatabaseConfiguration2()
    {       
        return database2;
        }
}

3) In this step, I should be updating the DropWizard service to handle the respective database instance.

public class DropWizardService extends Service<DropWizardConfiguration>{
    private final HibernateBundle<DropWizardConfiguration> hibernate = new HibernateBundle<DropWizardConfiguration>(Game.class) {

        // @Override
        public DatabaseConfiguration getDatabaseConfiguration(DropWizardConfiguration configuration){
            return configuration.getDatabaseConfiguration();
        }
    };

    @Override
    public void initialize(Bootstrap<DropWizardConfiguration> bootstrap) {  
        bootstrap.addBundle(hibernate);
    }

    public void run() throws Exception{
        this.run(new String[]{"server", "./config.yml"});
    }

    @Override
    public void run(DropWizardConfiguration configuration, Environment environment) throws Exception {
        SessionFactory factory = hibernate.getSessionFactory();     
        environment.addResource(new MyResource(factory));   
    }   
}

But this is where I'm going into a blackbox, I am not sure what configuration to change or How to change!

Appreciate some inputs.

Was it helpful?

Solution

I recently had to do something similar and the way I did it was by using BeanDefinitionRegistryPostProcessor. I used jdbcTemplates but it should be transferable to hibernate sessions aswel. Doing it this was I was able to add any amount of databases and the beans would get created. then just add the beans in the context file.

What I did was in the config

@Valid
@NotNull
@JsonProperty("database")
private Collection<DataSourceConfig> databases;

public Collection<DataSourceConfig> getDatabase() {
    return databases;
}

This is my DataSourceConfig class

public class DataSourceConfig {

@JsonProperty
private String name;

@JsonProperty
private String driverClassName;

@JsonProperty
private String username;

@JsonProperty
private String password;

@JsonProperty
private String url;

public String getDriverClassName() {
    return driverClassName;
}

public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

This is the method from my class that implements BeanDefinitionPostProcessor

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

    /**
     * Builds JdbcTemplates for beans
     */
    Map<String,String> jdbcTemplateBeanNames = Maps.newHashMap();
    for (DataSourceConfig dsc : configuration.getDatabase()) {
        AbstractBeanDefinition jdbcTemplateDefinition = BeanDefinitionBuilder.rootBeanDefinition("org.springframework.jdbc.core.JdbcTemplate")
                .addPropertyValue("dataSource",
                        BeanDefinitionBuilder.genericBeanDefinition(DriverManagerDataSource.class)
                            .addPropertyValue("driverClassName", dsc.getDriverClassName())
                            .addPropertyValue("url", dsc.getUrl())
                            .addPropertyValue("username", dsc.getUsername())
                            .addPropertyValue("password", dsc.getPassword())
                            .getBeanDefinition()
                )
                .getBeanDefinition();
        String name = BeanDefinitionReaderUtils.registerWithGeneratedName(jdbcTemplateDefinition, registry);
        jdbcTemplateBeanNames.put(dsc.getName(), name);

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top