Multiple Datasources in Spring Boot application

For Spring Boot applications that only work with one database, you can refer to the article Configure database in Spring Boot project using JPA Starter dependency to know how to configure the database for the application. In reality, sometimes we will work with applications that need to connect to 2 or more different databases. So in these cases, how can we connect to all these databases to work with them? I will guide you on how to do this in this tutorial!

First, I will create a new Spring Boot project with Spring Data JPA Starter, MySQL Driver, PostgreSQL Driver, and Lombok as an example:

MySQL Driver and PostgreSQL Driver will help my example application connect to the MySQL database and PostgreSQL database as an example!

As an example and for simplicity, I will define a table in MySQL database containing student information with 2 columns as follows:

Entity of table “student” is as follows:

The Repository class for this Student entity is as follows:

Similarly, I also defined a table in the PostgreSQL database containing class information with 2 columns as follows:

Entity of table “clazz” will be as follows:

The repository class for the Clazz entity is as follows:

Now, I will configure the information of these two databases.

I will create a new PostgreSQLDatabaseConfiguration class to configure the PostgreSQL database with the @EnableJpaRepositories annotation as follows:

The basePackages attribute will have the value of the package containing all repositories related to the PostgreSQL database, mine here is “com.huongdanjava.springboot.postgresql.repository”, guys! The values of the two attributes entityManagerFactoryRef and transactionManagerRef will point to the two beans that I will define for the PostgreSQL database as follows:

Due to some reasons related to auto-configuration, the EntityManagerFactoryBuilder class in the JpaBaseConfiguration class needs to determine the bean of the Datasource class to initialize itself in the Spring container. You need to specify one of the 2 Datasource beans for their 2 databases as primary by using Spring’s @Primary annotation, as I did above.

Information related to the PostgreSQL database I will configure in the file as follows:

Please note that we need to declare postgresql.datasource.jdbc-url instead of postgresql.datasource.url because by default Spring Boot will initialize HirakiDataSource. This HirakiDataSource class uses the HikariConfig class to store database configuration information. This HikariConfig class uses the jdbcUrl attribute to store information about the connection string!

Similar to PostgreSQL, for MySQL database, I will also create a new MySQLDatabaseConfiguration class to configure it as follows:

Information related to MySQL database, I will also configure in the file as follows:

Now, I will implement the CommandLineRunner interface for the SpringBootMultipleDatasourcesApplication class to retrieve student and class information from the above two databases as follows:

If my MySQL and PostgreSQL databases have the following information about classes and students:



then when you run the example, you will see the following results:

So we have successfully configured the Spring Boot application to connect and work with multiple databases at the same time!

Add Comment