12 Configurations

Micronaut features several built-in configurations that enable integration with common databases and other servers.

12.1 Configurations for Data Access

The table summarizes the configuration modules and the dependencies you should add to your build to enable them.

Table 1. Data Access Configuration Modules
DependencyDescription

io.micronaut.configuration:micronaut-jdbc-dbcp

Configures SQL DataSource instances using Commons DBCP

io.micronaut.configuration:micronaut-jdbc-hikari

Configures SQL DataSource instances using Hikari Connection Pool

io.micronaut.configuration:micronaut-jdbc-tomcat

Configures SQL DataSource instances using Tomcat Connection Pool

io.micronaut.configuration:micronaut-hibernate-jpa

Configures Hibernate/JPA EntityManagerFactory beans

io.micronaut.configuration:micronaut-hibernate-gorm

Configures GORM for Hibernate for Groovy applications

io.micronaut.configuration:micronaut-mongo-reactive

Configures the MongoDB Reactive Driver

io.micronaut.configuration:micronaut-mongo-gorm

Configures GORM for MongoDB for Groovy applications

io.micronaut.configuration:micronaut-neo4j-bolt

Configures the Bolt Java Driver for Neo4j

io.micronaut.configuration:micronaut-neo4j-gorm

Configures GORM for Neo4j for Groovy applications

io.micronaut.configuration:micronaut-postgres-reactive

Configures the Reactive Postgres Client

io.micronaut.configuration:micronaut-redis-lettuce

Configures the Lettuce driver for Redis

io.micronaut.configuration:micronaut-cassandra

Configures the Datastax Java Driver for Cassandra

For example, to add support for MongoDB you define the following dependency:

build.gradle

  1. compile "io.micronaut.configuration:micronaut-mongo-reactive"

For Groovy users, Micronaut provides special support for GORM.

For GORM for Hibernate you should not have both the hibernate-jpa and hibernate-gorm dependency.

The following sections go into more detail about configuration options and the exposed beans for each implementation.

12.1.1 Configuring a SQL Data Source

Java data sources can be configured for one of three currently provided implementations. Apache DBCP2, Hikari, and Tomcat are supported by default.

Configuring a JDBC DataSource

Using the CLI

If you are creating your project using the Micronaut CLI, supply one of the jdbc-tomcat, jdbc-hikari, or jdbc-dbcp features to preconfigure a simple JDBC connection in your project, along with a default H2 database driver:

  1. $ mn create-app my-app features jdbc-tomcat

To get started, simply add a dependency to one of the JDBC configurations that corresponds to the implementation you would like to use. Choose one of the following:

  1. runtime("io.micronaut.configuration:micronaut-jdbc-tomcat")
  1. <dependency>
  2. <groupId>io.micronaut.configuration</groupId>
  3. <artifactId>micronaut-jdbc-tomcat</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  1. runtime("io.micronaut.configuration:micronaut-jdbc-hikari")
  1. <dependency>
  2. <groupId>io.micronaut.configuration</groupId>
  3. <artifactId>micronaut-jdbc-hikari</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  1. runtime("io.micronaut.configuration:micronaut-jdbc-dbcp")
  1. <dependency>
  2. <groupId>io.micronaut.configuration</groupId>
  3. <artifactId>micronaut-jdbc-dbcp</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>

You also need to add a JDBC driver dependency to your classpath. For example to add the H2 In-Memory Database:

  1. runtime("com.h2database:h2")
  1. <dependency>
  2. <groupId>com.h2database</groupId>
  3. <artifactId>h2</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>

For more information see the Configuring JDBC section of the Micronaut SQL libraries project.

12.1.2 Configuring Hibernate

Setting up a Hibernate/JPA EntityManager

Using the CLI

If you are creating your project using the Micronaut CLI, supply the hibernate-jpa feature to include a Hibernate JPA configuration in your project:

  1. $ mn create-app my-app features hibernate-jpa

Micronaut features built in support for configuring a Hibernate / JPA EntityManager that builds on the SQL DataSource support.

Once you have configured one or many DataSources to use Hibernate, you will need to add the hibernate-jpa dependency to your build configuration:

  1. implementation("io.micronaut.configuration:micronaut-hibernate-jpa")
  1. <dependency>
  2. <groupId>io.micronaut.configuration</groupId>
  3. <artifactId>micronaut-hibernate-jpa</artifactId>
  4. </dependency>

For more information see the Configuring Hibernate section of the Micronaut SQL libraries project.

Using GORM for Hibernate

For Groovy users and users familiar with the Grails framework, special support for GORM for Hibernate is available. To use GORM for Hibernate you should not include Micronaut’s built in SQL Support or the hibernate-jpa dependency since GORM itself takes responsibility for creating the DataSource, SessionFactory etc.

Using the CLI

If you are creating your project using the Micronaut CLI, supply the hibernate-gorm feature to include GORM, a basic connection pool configuration, and a default H2 database driver in your project:

  1. $ mn create-app my-app features hibernate-gorm

See the GORM Modules section of the Micronaut for Groovy user guide.

12.1.3 Configuring MongoDB

Setting up the Native MongoDB Driver

Using the CLI

If you are creating your project using the Micronaut CLI, supply the mongo-reactive feature to configure the native MongoDB driver in your project:

  1. $ mn create-app my-app features mongo-reactive

Micronaut includes a configuration to automatically configure the native MongoDB Java driver. To use this configuration, add the following dependency to your application:

  1. implementation("io.micronaut.configuration:micronaut-mongo-reactive")
  1. <dependency>
  2. <groupId>io.micronaut.configuration</groupId>
  3. <artifactId>micronaut-mongo-reactive</artifactId>
  4. </dependency>

Then configure the URI of the MongoDB server in application.yml:

Configuring a MongoDB server

  1. mongodb:
  2. uri: mongodb://username:password@localhost:27017/databaseName
The mongodb.uri follows the MongoDB Connection String format.

A non-blocking Reactive Streams MongoClient is then available for dependency injection.

To use the blocking driver, add a dependency to your application to the mongo-java-driver.

  1. compile "org.mongodb:mongo-java-driver"

Then the blocking MongoClient will be available for injection.

See the Micronaut MongoDB documentation for further information on configuring and using MongoDB within Micronaut.

12.1.4 Configuring Neo4j

Micronaut features dedicated support for automatically configuring the Neo4j Bolt Driver for the popular Neo4j Graph Database.

Using the CLI

If you are creating your project using the Micronaut CLI, supply the neo4j-bolt feature to configure the Neo4j Bolt driver in your project:

  1. $ mn create-app my-app features neo4j-bolt

To configure the Neo4j Bolt driver you should first add the neo4j-bolt module to your classpath:

  1. implementation("io.micronaut:micronaut-neo4j-bolt")
  1. <dependency>
  2. <groupId>io.micronaut</groupId>
  3. <artifactId>micronaut-neo4j-bolt</artifactId>
  4. </dependency>

You should then configure the URI of the Neo4j server you wish to communicate with in application.yml:

Configuring neo4j.uri

  1. neo4j:
  2. uri: bolt://localhost
The neo4j.uri setting should be in the format as described in the Connection URIs section of the Neo4j documentation

Once you have the above configuration in place you can inject an instance of the org.neo4j.driver.v1.Driver bean, which features both a synchronous blocking API and a non-blocking API based on CompletableFuture.

See the Micronaut Neo4j documentation for further information on configuring and using Neo4j within Micronaut.

12.1.5 Configuring Postgres

Micronaut supports reactive and non-blocking client to connect to Postgres using reactive-pg-client, allowing to handle many database connections with a single thread.

Configuring the Reactive Postgres Client

Using the CLI

If you are creating your project using the Micronaut CLI, supply the postgres-reactive feature to configure the Reactive Postgres client in your project:

  1. $ mn create-app my-app features postgres-reactive

To configure the Reactive Postgres client you should first add postgres-reactive module to your classpath:

build.gradle

  1. compile "io.micronaut.configuration:micronaut-postgres-reactive"

For more information see the Configuring Reactive Postgres section of the Micronaut SQL libraries project.

12.1.6 Configuring Redis

Micronaut features automatic configuration of the Lettuce driver for Redis via the redis-lettuce module.

Configuring Lettuce

Using the CLI

If you are creating your project using the Micronaut CLI, supply the redis-lettuce feature to configure the Lettuce driver in your project:

  1. $ mn create-app my-app features redis-lettuce

To configure the Lettuce driver you should first add the redis-lettuce module to your classpath:

build.gradle

  1. compile "io.micronaut.configuration:micronaut-redis-lettuce"

You should then configure the URI of the Redis server you wish to communicate with in application.yml:

Configuring redis.uri

  1. redis:
  2. uri: redis://localhost
The redis.uri setting should be in the format as described in the Connection URIs section of the Lettuce wiki

You can also specify multiple Redis URIs using redis.uris in which case a RedisClusterClient is created instead.

For more information and further documentation see the Micronaut Redis documentation.

12.1.7 Configuring Cassandra

Using the CLI

If you are creating your project using the Micronaut CLI, supply the cassandra feature to include Cassandra configuration in your project:

  1. $ mn create-app my-app features cassandra

For more information see the Micronaut Cassandra Module documentation.

12.1.8 Configuring Liquibase

To configure Micronaut integration with Liquibase, please follow these instructions.

12.1.9 Configuring Flyway

To configure Micronaut integration with Flyway, please follow these instructions