Chapter 1. Introduction to using data sources with Quarkus

When you want to add persistent data storage to your application, you must connect your application to a relational database. To achieve this, you use a data source that you connect to your application using a database driver. You can connect your Quarkus application to one or multiple data sources. You can use the data source management functionalities integrated into Quarkus to:

  • configure your application to use one or multiple data sources
  • obtain references to data sources in your code
  • view and set pool tuning configuration properties

In Quarkus applications, you can use two types of database drivers to connect your application to a relational database. You can use multiple data sources of both types in one application simultaneously:

JDBC drivers
that use the standard JDBC API that provides database connectivity for Java-based application. Quarkus JDBC drivers manage database connections using Agroal, a fast, light-weight, and highly scalable database connection pool implementation that is integrated with other features of Quarkus, including security, transaction management and health checks.
Reactive drivers
that are based on the data source driver implementation in Eclipse Vert.x. Eclipse Vert.x reactive data source drivers provide the non-blocking and reactive network-related features of Quarkus and are suitable for applications that are designed to be highly scalable and event-driven.

You can configure both types of data source drivers using a set of unified and flexible configuration options that Quarkus provides.

1.1. Setting the db-kind property for a data source

When you set the db-kind property in the configuration file of your application to match the type of your data source that you want to use, Quarkus automatically resolves the appropriate type of database driver. The following procedure demonstrates how you can set the db-kind property for a data source.

Prerequisites

  • You have a Quarkus Maven project.

Procedure

  1. Navigate to your Quarkus project directory.
  2. In the src/main/resources/application.properties file, set the value of the db-kind property to match the type of the data source that you want to use. The following example uses postgresql as the data source type:

    quarkus.datasource.db-kind=postgresql

1.2. Setting database credentials

You can define credentials that your application uses to access your database. Defining access credentials for your database is optional. You can skip this procedure when configuring a data source for your application.

Prerequisites

  • You have a Quarkus Maven project.
  • You have set the db-kind property for your data source.
  • Your Quarkus application is in JVM mode. This prerequisite applies when you use a JDBC driver that Quarkus does not support in native mode.

Procedure

  1. Navigate to your Quarkus project directory.
  2. In the src/main/resources/application.properties file, set the value of the quarkus.datasource.username property to match the username that your application uses to access the database:

    quarkus.datasource.username=<username>
  3. In the src/main/resources/application.properties file, set the value of the quarkus.datasource.password property to match the password that your application uses to access the database:

    quarkus.datasource.password=<password>

1.3. Quarkus driver extensions for built-in databases

The following table provides an overview of Quarkus built-in databases and the extensions that you can use to connect your application to a relational database:

Table 1.1. Driver extensions for Quarkus built-in databases

Database built-indb-kindAgroal extensionReactive extension

DB2

db2

quarkus-jdbc-db2

quarkus-reactive-db2-client

Derby

derby

quarkus-jdbc-derby

N/A

H2

h2

quarkus-jdbc-h2

N/A

MariaDB

mariadb

quarkus-jdbc-mariadb

quarkus-reactive-mysql-client

Microsoft SQL Server

mssql

quarkus-jdbc-mssql

N/A

MySQL

mysql

quarkus-jdbc-mysql

quarkus-reactive-mysql-client

PostgreSQL

postgresql, pgsql, or pg

quarkus-jdbc-postgresql

quarkus-reactive-pg-client

You can configure H2 and Derby databases to run in embedded mode. The H2 and Derby driver extensions do not support compiling the embedded database engine into native executables.

Important

This table includes supported and community artifacts. For a list of supported Maven artifacts, see the Red Hat build of Quarkus Component Details page.

When you use one of the built-in database kinds the JDBC driver resolves automatically to the following values:

Table 1.2. JDBC and XA drivers for Quarkus built-in databases

DatabaseJDBC driverXA driver

DB2

com.ibm.db2.jcc.DBDriver

com.ibm.db2.jcc.DB2XADataSource

Derby

org.apache.derby.jdbc.ClientDriver

org.apache.derby.jdbc.ClientXADataSource

H2

org.h2.Driver

org.h2.jdbcx.JdbcDataSource

MariaDB

org.mariadb.jdbc.Driver

org.mariadb.jdbc.MySQLDataSource

Microsoft SQL Server

com.microsoft.sqlserver.jdbc.SQLServerDriver

com.microsoft.sqlserver.jdbc.SQLServerXADataSource

MySQL

com.mysql.cj.jdbc.Driver

com.mysql.cj.jdbc.MysqlXADataSource

PostgreSQL

org.postgresql.Driver

org.postgresql.xa.PGXADataSource

Note

You can configure H2 and Derby databases to run in embedded mode. The H2 and Derby driver extensions do not support compiling the embedded database engine into native executables.