Spring Boot + Oracle Database

In this article, we’ll explore several Oracle Database integrations with Spring Boot from the Spring Cloud Oracle project.

Try out any of the provided samples with Oracle Database Free.

UCP DataSource over Hikari

Illustration depicting a red database with a cloud backdrop and a square labeled 'JDBC'.

If you’re familiar with Spring Boot JDBC, you’ll know that Hikari is the default for connection pools. However, if you’re looking to use advanced database features and get the most out of your database connection pool, consider Oracle’s Universal Connection Pool (UCP).

UCP is designed to work seamlessly within the Oracle ecosystem, providing better support and optimizations for Oracle Database than Hikari.

  • Support for Oracle features like Real Application Clusters (RAC), Data Guard, Database Resident Connection Pooling (DRCP), Fast Application Notification (FAN), Transparent Application Continuity (TAC), sharding, load balancing, and more!
  • Configuring UCP with Spring application properties is straightforward.

To add UCP to your Spring project, add the Oracle UCP Spring Boot Starter dependency:

<dependency>
    <groupId>com.oracle.database.spring</groupId>
    <artifactId>oracle-spring-boot-starter-ucp</artifactId>
</dependency>

Then configure Spring to use UCP with Oracle Database:

spring:
  datasource:
    url: ${spring.datasource.url}
    username: ${spring.datasource.username}
    password: ${spring.datasource.password}
    driver-class-name: oracle.jdbc.OracleDriver
    # Use the UCP Pool DataSource
    type: oracle.ucp.jdbc.PoolDataSource
    oracleucp:
      # Your UCP connection properties here!
      connection-factory-class-name: oracle.jdbc.pool.OracleDataSource
      connection-pool-name: MyConnectionPool
      initial-pool-size: 15
      min-pool-size: 10
      max-pool-size: 30
      shared: true

That’s all you need for the basics of getting started with UCP! For more information, check out this great article by Juarez Junior about switching from Hikari to UCP.

Read in-depth on UCP vs. Hikari for Spring Boot here.

Also, find a sample project using UCP with JPA here.

Oracle Wallet

If you’re using Oracle Wallet to secure database connections, add the oracle-spring-boot-starter-wallet dependency to your project:

<dependency>
    <groupId>com.oracle.database.spring</groupId>
    <artifactId>oracle-spring-boot-starter-wallet</artifactId>
</dependency>

This dependency bundles all the necessary wallet-related dependencies together, making mTLS with Oracle Database simpler.

JMS

Java Message Service (JMS) is an API that provides a standardized way for Java applications to create, send, receive, and read messages in messaging systems. Spring JMS uses idiomatic APIs to produce and consume messages with JMS, using the JMSTemplate class and @JMSListener annotations for producers and consumers, respectively.

Spring JMS easily integrates with Oracle Database Transactional Event Queues – to get started, add the oracle-spring-boot-starter-aqjms to your project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>com.oracle.database.spring</groupId>
    <artifactId>oracle-spring-boot-starter-aqjms</artifactId>
</dependency>

Then, you can use Spring JMS constructs like @JMSListener or JMSTemplate to produce and consume messages in your application:

@JmsListener(destination = "${txeventq.queue.name:testqueue}", id = "sampleConsumer")
public void receiveMessage(String message) {
    System.out.printf("Received message: %s%n", message);
    latch.countDown();
}

Click here for an in-depth sample using Spring JMS with Oracle Database Transactional Event Queues.

Spring Cloud Stream Binder

Part of the Spring Cloud Stream umbrella, the Oracle TxEventQ Stream Binder provides Spring streaming APIs for using Transactional Event Queues within Oracle Database. The Stream Binder uses TxEventQ’s JMS API to produce and consume messages using Oracle Database.

You can find a sample application using the Oracle TxEventQ Stream Binder here

If you’re not familiar with Transactional Event Queues, they’re conceptually similar to Kafka topics, promoting a pub-sub interface that resides completely within the database, removing the need for an external message bus!

kafka-clients APIs

The open-source okafka project implements kafka-clients APIs for Oracle Database Transactional Event Queues (TxEventQ), allowing it to serve as a drop-in replacement.

The oracle-spring-boot-starter-okafka adds basic integration with Spring Boot. You can add it to your Java project like so:

<dependency>
    <groupId>com.oracle.database.spring</groupId>
    <artifactId>oracle-spring-boot-starter-okafka</artifactId>
    <version>${oracle-spring-boot-starter.version}</version>
</dependency>

Then, you can start using Kafka APIs with Oracle Database in your Spring Boot applications.

Check out a sample application using the Spring Boot okafka starter here, or learn more about event streaming with Oracle Database.

What’s next for okafka and Spring Boot? Within the next few okafka releases, I’m hoping to add full compatibility for spring-kafka!

JSON Collections

The JSON Collections database starter includes necessary dependencies to work with JSON data in Oracle Database. If you’re using the JSON column type, Duality. Views, or other JSON features, this dependency supports working with JSON data in Oracle Database

To add it to your project, use the following dependency:

<dependency>
    <groupId>com.oracle.database.spring</groupId>
    <artifactId>oracle-spring-boot-starter-json-collections</artifactId>
</dependency>

You can then use several provided serialization utilities, or the JSONBRowMapper.

A neat sample shows this off by combining Oracle JSON data (OSON) with event streaming – all within the database:

Sample for JSON Events and the Kafka Java Client for Oracle Database Transactional Event Queues

Testcontainers with Oracle Database

The Testcontainers library makes it easy to run containers with Java, and is integrated directly with Spring.

The Oracle Database Testcontainers dependency allows you to run Oracle Database containers from your Java tests (for free!). This is one of my favorite features of Spring+Oracle, helping me run high-quality tests with minimal resources.

To get started with Oracle Database 26ai and Testcontainers, add the following dependencies to your project:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc11</artifactId>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ucp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>testcontainers-junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>testcontainers-oracle-free</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Then start writing tests with Oracle Database!

@Testcontainers
public class MyAppTest {
    static String dbImage = "gvenzl/oracle-free:23.6-slim-faststart";

    // The Oracle Database container automatically starts up with each test
    // run, and is terminated after the test completes.
    @Container
    static OracleContainer oracleContainer = new OracleContainer(dbImage)
            .withStartupTimeout(Duration.ofMinutes(2))
            .withUsername("testuser")
            .withPassword(("testpwd"));

    // Write your data application tests here!
}

Click here for a collection of samples using Testcontainers with Oracle Database.

References

Life’s better with code samples. Use these resources to get started:

Response

  1. […] Spring Boot + Oracle Database – Anders Swanson demos Spring Boot with Oracle […]

Leave a Reply

Discover more from andersswanson.dev

Subscribe now to keep reading and get access to the full archive.

Continue reading