Multiple Databases With Spring Boot and Spring Data Jpa
Posted on January 5, 2016 • 2 min read • 423 wordsA little while back I knocked up a post describing how to enable a Spring application to connect to multiple data sources. At the time, I had only just heard about Spring Boot at the SpringOne 2GX conference in Santa Clara, so the examples didn’t take advantage of that and also didn’t work around some of the autowiring that it does.
Recently, I was working on a little ETL project to migrate data from one database to another with a different structure, so I returned to this problem and the following is the result.
First, if you want to get hold of a working (including some simple tests) example project, here it is:
https://github.com/gratiartis/multids-demo/tree/now-with-spring-boot
As previously, when you define an entity manager, you can define where it should scan for entities and repository classes. The classes can be named individually, but it is easiest if you put your domain entities and repository classes into their own packages and point the entity manager factory at the package. In this example, I used:
com.sctrcd.multids.foo.domain
com.sctrcd.multids.foo.repo
com.sctrcd.multids.bar.domain
com.sctrcd.multids.bar.repo
I suspect that it’s certainly possible to get around it, but I found that due to Spring Boot trying to inject beans
based on default names, it was easiest to set up one of the data sources to use the defaults and the other to use bean
names that I defined. As you can see in the application.yml
below:
… the spring.datasource.url
, spring.datasource.username
and spring.datasource.password
properties are all defined
for the ‘default’ datasource. I define some additional non-conventional properties for the additional schema. We will
see how those are picked up shortly.
Beyond the application.yml
configuration, all we need to do is define @Configuration
beans which will pick up the
properties. First, a @Configuration
to wire up the ‘default’ data source. This defines each bean as @Primary
, to
ensure that they are the beans picked up by anything which does not specify a @Qualifier
:
Second a @Configuration
to wire up the additional datasource. It is essentially identical to the ‘default’
configuration, except that it defines non-conventional names for the data source, entity manager factory and transaction
manager and scans different packages for the entities and repositories. It also defines the named transaction manager in
the @EnableJpaRepositories
annotation and does not define the beans as @Primary
.
Beyond those configuration classes, everything is just the standard setup for a Spring Boot / Spring Data JPA application, so if you have an application connecting to a single database already, there isn’t a lot of modification to support connecting to additional databases.