Using Multiple Datasources With SpringBoot Application

Using Multiple Datasources With SpringBoot Application

Project Creation

We can use Spring Initializer to create the example application with the dependencies below:

No alt text provided for this image

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:

No alt text provided for this image

Properties File

First, let’s define two different data source settings in the application.properties file like below:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

I’ve made two different implementations of this interface because even it’s a simple application, a real-life scenario would be different.

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

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:

No alt text provided for this image
No alt text provided for this image

Exception Handling

I’ve created CustomException class below and used the central exception handling with the ControllerAdvice:

No alt text provided for this image
No alt text provided for this image

Controller

I’ve created two controllers. One is for billing and one is for customer requests; inject both service classes into controllers.

No alt text provided for this image
No alt text provided for this image

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

Kartik Ahalawat

Assistant System Engineer @TCS

3y

Thank you for sharing this useful information

Ahmed Aly 🇵🇸

System & Platform Engineer | CKA Certified | RHCSA Certified | 2x AWS Certified

3y

Great 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?

Hany Nathan

Software Development Manager - Agile kanban (Helping my team ship software that worked as expected, on schedule, and within budget)

3y

Hi 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

Mohammad AbuMuhasien

Senior Java Developer |e-commerce Java | Spring framework | Backend Development|Payment

3y

Keep going omar👍💪

To view or add a comment, sign in

Others also viewed

Explore topics