Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

A database and Dev Services

So far posts live in memory and disappear on every restart. Time to connect a database. Two extensions are needed: Hibernate ORM with Panache, for working with tables through Java classes, and the JDBC driver of a particular database. In Quarkus you usually do not start with a database address and password, but with the question whether any settings are needed at all: in dev mode Dev Services starts the database for you.

Adding the extensions

./mvnw quarkus:add-extension -Dextensions="hibernate-orm-panache,jdbc-h2"

quarkus-hibernate-orm-panache and quarkus-jdbc-h2 appear in pom.xml. H2 is a small database written in Java. It runs right inside the application and needs nothing installed, so it is handy for learning. Real servers more often use PostgreSQL — the jdbc-postgresql extension.

What Dev Services is

Dev Services is a Quarkus feature that, in the dev and test modes, starts the services you need by itself: a database, a message broker, Redis. There is one condition: the service address is not set in the configuration. Quarkus sees the driver in the dependencies, sees that quarkus.datasource.jdbc.url is not set, and starts a database itself.

How it starts it depends on the database:

DatabaseHow Dev Services starts itDocker needed
H2, DerbyInside the applicationNo
PostgreSQL, MySQL, MariaDB and othersIn a container through TestcontainersYes (or Podman)

We use H2, so Docker is not needed. If you have Docker installed, you can take jdbc-postgresql: Dev Services then downloads a PostgreSQL image and runs it in a container — the code of the lessons stays the same.

Starting

The application.properties file says nothing about the database. Once the project has its first entity (we create it in the next lesson), ./mvnw quarkus:dev prints at startup (output shortened):

INFO  [io.quarkus.hibernate.orm.deployment.dev.HibernateOrmDevServicesProcessor] Setting [quarkus.hibernate-orm.schema-management.strategy]=drop-and-create to initialize Dev Services managed database
ERROR [org.testcontainers.dockerclient.DockerClientProviderStrategy] Could not find a valid Docker environment. Please check configuration.
INFO  [io.quarkus.datasource.deployment.devservices.DevServicesDatasourceProcessor] Dev Services for default datasource (h2) started
INFO  [io.quarkus] Installed features: [agroal, cdi, compose, hibernate-orm, hibernate-orm-panache, hibernate-validator, jdbc-h2, narayana-jta, rest, rest-jackson, ...]

The line Dev Services for default datasource (h2) started means the database is running. The ERROR line about Docker appears on a machine without Docker: Quarkus checks whether Docker is there, but H2 does not need it, and the application carries on.

The table schema

The first log line mentions drop-and-create: on every start Hibernate drops the tables and creates them again from the Java classes. This is convenient for development — the schema always matches the code. But the data disappears on restart, so in the next lesson we fill the database at startup.

schema-management.strategy valueWhat it does
drop-and-createRecreates the tables at startup
updateAdds new tables and columns, keeps the data
validateOnly checks that the schema matches
noneDoes nothing

A database for the server

Dev Services does not work in the prod profile, so the address must be set explicitly. For the learning project we use H2 in a file:

# src/main/resources/application.properties
%prod.quarkus.datasource.jdbc.url=jdbc:h2:file:./data/blog
%prod.quarkus.hibernate-orm.schema-management.strategy=update

The database is stored in the data/blog.mv.db file next to where the application is started. For PostgreSQL on a server the settings look like this, and the password is passed in the QUARKUS_DATASOURCE_PASSWORD environment variable:

# src/main/resources/application.properties
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/blog
%prod.quarkus.datasource.username=blog
%prod.quarkus.datasource.password=changeme

Common mistake

If you run the built application without a database address, it does not start:

Unable to find datasource '<default>' for persistence unit '<default>': Bean is not active
Reason: Datasource '<default>' was deactivated automatically because its URL is not set.
To activate the datasource, set configuration property 'quarkus.datasource.jdbc.url'.

In dev everything worked thanks to Dev Services, but prod has none. Set %prod.quarkus.datasource.jdbc.url in the file or the QUARKUS_DATASOURCE_JDBC_URL environment variable.

Thanks for reading!

Donate to the site — we work hard for you.