Using Multiple Datasources With SpringBoot Application
Project Creation
We can use Spring Initializer to create the example application with the dependencies below:
After that selection, please push the Generate button and save the project as a zip file.
I use Eclipse as Java IDE, but you could use whichever you prefer to develop the sample application. Open the zip file, and import the folder as a Maven project within Eclipse. The project structure should be the same as below:
Properties File
First, let’s define two different data source settings in the application.properties file like below:
As I mentioned before, I’ve used the Oracle database but you could use other databases as long as you change the driverClassName accordingly.
Datasource Configuration
In order to create DataSource beans, I’ve created a DatasourceConfig class and annotated it as configuration and added two data sources as two different beans:
Data Access
As data access, I’ve used JdbcTemplate. To create the DAO interface, please add the IDatabaseDAO interface under the com.tech.multipledatasources.dao package as below:
I’ve made two different implementations of this interface because even it’s a simple application, a real-life scenario would be different.
For the database.sql.databasename variable, please add the “SELECT dbid, name FROM v$database” value in the application.properties file. Note that this SQL is also valid for the Oracle database. If you plan to use a different database, you need to change this statement.
I’ve used the constructor level dependency injection to inject the data sources and I've used Qualifier annotation to specify the bean name.
Domain
I also use DatabaseInfo model class to map SQL results into an object.
In order to keep the code clean and neat, I use Lombok.
Service
As the service layer, I’ve added two services classes as below, annotated the as Service, and injected the related DAO classes:
Exception Handling
I’ve created CustomException class below and used the central exception handling with the ControllerAdvice:
Controller
I’ve created two controllers. One is for billing and one is for customer requests; inject both service classes into controllers.
Running the Application
After running the application and the request, it will return the result, which will indicate the databases we just made in the connection.
Thanks To : Ibrahim Ersin Ozis
Assistant System Engineer @TCS
3yThank you for sharing this useful information
System & Platform Engineer | CKA Certified | RHCSA Certified | 2x AWS Certified
3yGreat efforts ya Omar, Would u suggest when should I learn about spring boot framework and what are the best resources for it other than its documentation?
Software Development Manager - Agile kanban (Helping my team ship software that worked as expected, on schedule, and within budget)
3yHi Omar it's a good effort but I noticed this "a real-life scenario would be different" and anyone who reads your blog wants to learn the real-life scenario please make this in your consideration and again thank you for your effort
Senior Java Developer |e-commerce Java | Spring framework | Backend Development|Payment
3yKeep going omar👍💪