In my post "SpringBoot - Use Multiple Data Sources like DB2, MySQL, Postgres, MariaDB " we have seen how to use Multiple Data Source like MySQL, Postgres, MariaDB within the same Spring Boot Application which should be using single configuration file with a data source connection pooling mechanism.
In this example we are going to see Use Multiple Data Sources like MySQL, Postgres, MariaDB in Quarkus application using AgroalDataSource and create multiple data source at runtime.
Prerequisites To complete this example:
- An IDE
- JDK 11+ installed with JAVA_HOME configured appropriately
- Apache Maven 3.8.1+
1. Create a maven project using Quarkus and add the below Dependencies in pom.xml. Here we have added quarkus-jdbc-mysql for mySql datasource, quarkus-jdbc-postgresql for postgres data source , quarkus-jdbc-mariadb for mariadb data source and quarkus-agroal for getting AgroalDataSource to create Data Source at Runtime.
When you configure multiple data sources for your application, ensure that you add the io.quarkus.agroal.DataSource qualifier with the name of the data source as the value to each DataSource class:
@Inject
AgroalDataSource defaultDataSource;
@Inject
@DataSource("mysqldb")
AgroalDataSource mysqlDataSource;
@Inject
@DataSource("mymaria")
AgroalDataSource mariaDataSource;
Here we have created the Rest End Point to getAllCountries list from postgres datasource. In the same way will create for each mysql and maridb.
4. run Quarkus application in dev using below command
5. Quarkus Application will be started with below logs and you will be able see our data source driver also loaded sussessfully
2022-08-07 12:38:46,512 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2022-08-07 12:38:46,514 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, jdbc-mariadb, jdbc-mysql, jdbc-postgresql, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation, vertx]
6. Now its time to test our implementation, for this we will use postman tool :
- request and response for mySql data source in postman Get request
Open mySql WorkBench and execute the below query, we can see we got the correct response from our Rest API getCountries.
Open pgAdmin Console and execute the below query, we can see we got the correct response from our Rest API getCountries.
- request and response for mariadb data source in postman Get request
Open MariaDB command promt and execute the below query, we can see we got the correct response from our Rest API getCountries.
Summary:
So in very simple steps we have seem how to Use Multiple Data Sources like MySQL, Postgres, MariaDB using AgroalDataSource Named Data source Injection at Runtime in Quarkus application.
Note : In this Example we have used very basic java way for creation of Data Source Connection, Execution of SQL Query and retrieving the Result Set as Rest Response.
In the Upcoming Article we will see how we can use Use Multiple Data Sources using single AgroalDataSourceConfiguration , AgroalDataSource ConnectionPool and route different data source at Runtime.
Till Then Happy Learning !!!
GitHub Link : rajivksingh13/teachlea
Please feel free to provide your valuable comments, Thanks.
0 Comments