2

I want to have an optional secondary database for my application such that if the secondary database exists on the deployed environment a repository can retrieve data from it, but if it does not exist, the application will still work fine with just the primary database, and the repository will behave in a way where the application can know this datasource isn't available.

I can definitely think of ways of accomplish this type of thing, but I wonder if there is a more direct configuration type way of achieving this. Or if there is a pattern type way of handling that is preferable.

user123959
  • 1,226
  • 1
  • 12
  • 29

1 Answers1

5

You can certainly work with multiple databases in a clean way by just implementing 2 database configurations and annotating one of them with @Primary annotation.

@Bean(name = "mainDataSource")
@Primary
public DataSource mainDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
}

And for the secondary database, you can create pretty much the same, but don't put the @Primary annotation on it.

@Bean(name = "secondaryDataSource") 
public DataSource secondaryDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
}

Note that you don't need to put the @Primary annotation just on the DataSource bean, but in any bean you use to connect to your database.

After doing this, all you have to do is annotate your EntityManager or any other connection manager you normally use with the @Qualifier.

@Autowired
@Qualifier("mainEntityManager")
private EntityManager mainDatabase;

@Autowired
@Qualifier("secondEntityManager")
private EntityManager secondDatabase;

In my case, I prefer to make a class to manage this on the backstage. You can find a complete example of this on my article on linkedin. and check out the example code on GitHub

  • 1
    Thanks for your answer. How will I handle the case of the second database not existing? Will spring still load up fine? And will there be a way for me to handle this situation programatically? – user123959 Sep 05 '17 at 13:52
  • 1
    Even if your application fail to connecto to the database during the boot, the application will start as expected. After that you can handle the checks and if the second one fails, you can change to the first. You can also try to change the JDBC Connection pool by hand to try to avoid this at the boot time if you like, check this [example to help you figure how](https://stackoverflow.com/a/23507135/4835819), but in your case, I would choose to point the primary DB to a more reliable server just to skip this kind of error message. – Paulo 'PaulusHC' Cruz Sep 05 '17 at 14:40