Chapter 2. JDBC data source configuration
JDBC is the database connection API most commonly used in Java-based applications. You can use a JDBC data source driver to connect your application to a relational database.
To configure a JDBC data source, you must
-
add the
quarkus-agroal
extension to your application -
add a
db-kind
extension to your application - specify the JDBC URL that your application uses to access the data source
The following example shows how you can connect a postgresql
data source to your application and specify the access credentials and the JDBC URL for the data source. For more information on how to specify the JDBC URL see Setting the JDBC URL of your data source.
Example of JDBC data source configuration
quarkus.datasource.db-kind=postgresql quarkus.datasource.username=<your_username> quarkus.datasource.password=<your_password> quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/hibernate_orm_test quarkus.datasource.jdbc.max-size=16
2.1. Installing Quarkus extensions for JDBC data sources
You must install the quarkus-agroal
extension and a Quarkus JDBC database driver extension to configure a JDBC data source. The JDBC database driver that you add must match the type of JDBC database that you want to use.
The following procedure demonstrates how to install Quarkus extensions for JDBC data sources.
Prerequisites
- You have a Quarkus Maven project.
-
You have set the
db-kind
property for your data source.
Procedure
- Navigate to your Quarkus project directory.
Add the
quarkus-agroal
extension to your project:./mvnw quarkus:add-extension -Dextensions="agroal"
Add the Quarkus extension for the appropriate type of relational database driver to your application:
./mvnw quarkus:add-extension -Dextensions="<extension>"
For example, to add a PostgreSQL database driver extension, use:
./mvnw quarkus:add-extension -Dextensions="jdbc-postgresql"
If you are using Hibernate ORM, you do not need to add the Agroal extension dependency explicitly. Agroal is a transitive dependency of the Hibernate ORM extension. You must use a JDBC data source driver with Hibernate ORM.
2.2. Setting the JDBC URL of your data source
You must set the JDBC URL to complete the configuration of your JDBC data source. The following procedure demonstrates how to set the JDBC URL.
Prerequisites
- You have a Quarkus Maven project.
- You have added the corresponding data source driver to your application.
-
You have set the
db-kind
property for your data source.
Procedure
- Navigate to your Quarkus project directory.
-
Open the
src/main/resources/application.properties
file. Set the value of the
quarkus.datasource.jdbc.url
property to match the JDBC URL for your data source:quarkus.datasource.jdbc.url=<JDBC_URL>
For example, to set the JDBC URL of a PostgreSQL data source, use:
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/hibernate_orm_test
2.3. Obtaining a JDBC data source with Hibernate ORM
If you are using Hibernate ORM, your application automatically recognizes and connects to an available JDBC data source. To access the data source in your application code, you can obtain it as a CDI bean.
Prerequisites
- You have a Quarkus Maven project.
- You have installed Hibernate ORM.
Procedure
To access the data source in your application code, add the
@Inject
annotation to thedataSource
property:import javax.sql.DataSource; @Inject DataSource dataSource;
2.4. Disabling a JDBC data source in a simultaneous configuration
You can simultaneously configure a JDBC driver extension and a reactive driver extension for the same data source in your application. You can disable the JDBC data source driver in a simultaneous configuration, thus forcing your application to use the reactive data source driver.
Prerequisites
- You have a Quarkus Maven project.
- You have a JDBC data source driver and a reactive data source driver configured simultaneously in your application.
Procedure
- Navigate to your Quarkus project directory.
-
Open the
src/main/resources/application.properties
file. Set the
quarkus.datasource.jdbc
property tofalse
to disable the JDBC data source:quarkus.datasource.jdbc=false
2.5. Configuring JDBC drivers with no built-in driver extension
You can use JDBC drivers and databases that do not have built-in driver extensions. The following procedure demonstrates how to configure a JDBC driver with no built-in extension.
You can use any JDBC driver while using your Quarkus application in JVM mode. The non-included JDBC drivers may not function properly when you compile your Quarkus as a native executable.
Prerequisites
- You have a Quarkus Maven project.
- Your Quarkus application is in JVM mode.
-
You add the data source driver that you want to use as a dependency in the
pom.xml
file of your project.
Procedure
- Navigate to your Quarkus project directory.
-
Open the
src/main/resources/application.properties
file. Set the value of the
db-kind
property toother
:quarkus.datasource.db-kind=other
Set the value of the
quarkus.datasource.jdbc.driver
property to match the type of driver extension that you want to use:quarkus.datasource.jdbc.driver=<driver>
Set the value of the
quarkus.datasource.jdbc.url
property to match the JDBC URL for your data source:quarkus.datasource.jdbc.url=<JDBC_URL>
For example, Quarkus does not provide a built-in driver extension for the OpenTracing driver. Ensure that you add the
opentracing-jdbc
artifact to yourpom.xml
file, and set the following properties in yourapplication.properties
file to configure the OpenTracing driver:quarkus.datasource.db-kind=postgresql quarkus.datasource.jdbc.url=jdbc:tracing:postgresql://localhost:5432/mydatabase quarkus.datasource.jdbc.driver=io.opentracing.contrib.jdbc.TracingDriver quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQLDialect
2.6. Configuring multiple JDBC data sources
You can configure multiple JDBC data sources for your Quarkus application with Agroal.
When you use the Hibernate ORM extension, you can add only one persistent data storage unit to your application. The persistent data storage unit managed by the Hibernate ORM extension uses the default JDBC data source type.
Prerequisites
- You have a Quarkus Maven project.
- You have multiple JDBC data sources.
Procedure
- Navigate to your Quarkus project directory.
In the
src/main/resources/application.properties
file, set the following properties for each of your data sources:-
The name of the data source. For example, if you choose
users
as the name of the data source, the fully qualified reference of your data source isquarkus.datasource.users
. -
The
db-kind
property that matches the type of the data source -
The
quarkus.datasource.username
that specifies the username for the data source -
The
quarkus.datasource.jdbc.url
that specifies the JDBC connection URL of the data source -
The
quarkus.datasource.jdbc.min-size
property that specifies the minimum number of connections that are maintained in the connection pool of the data source The
quarkus.datasource.jdbc.max-size
property that specifies the maximum number of connections that are maintained in the connection pool of the data sourceThe following example shows the complete configuration for 3 JDBC data sources:
quarkus.datasource.db-kind=h2 quarkus.datasource.username=username-default quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:default quarkus.datasource.jdbc.min-size=3 quarkus.datasource.jdbc.max-size=13 quarkus.datasource.users.db-kind=h2 quarkus.datasource.users.username=username1 quarkus.datasource.users.jdbc.url=jdbc:h2:tcp://localhost/mem:users quarkus.datasource.users.jdbc.min-size=1 quarkus.datasource.users.jdbc.max-size=11 quarkus.datasource.inventory.db-kind=h2 quarkus.datasource.inventory.username=username2 quarkus.datasource.inventory.jdbc.url=jdbc:h2:tcp://localhost/mem:inventory quarkus.datasource.inventory.jdbc.min-size=2 quarkus.datasource.inventory.jdbc.max-size=12
-
The name of the data source. For example, if you choose
Inject multiple data sources into by adding the
@Inject
annotation to each one of the class properties. When you configure multiple data sources for your application, ensure that you add theio.quarkus.agroal.DataSource
qualifier with the name of the data source as the value to eachDataSource
class:import javax.inject.Inject; import io.agroal.api.AgroalDataSource; import io.quarkus.agroal.DataSource; @Inject AgroalDataSource defaultDataSource; @Inject @DataSource("users") AgroalDataSource usersDataSource; @Inject @DataSource("inventory") AgroalDataSource inventoryDataSource;
Additional resources