X

Break New Ground

Configuring the Oracle JDBC drivers in a SpringBoot project - Take 2

Last year I blogged on how you could configure the Oracle JDBC drivers in a Spring Boot project. At the time, Spring boot 2.3.0.RELEASE did not include explicit integration with Oracle UCP, the preferred connection pool when connecting to Oracle databases. However, that wasn't much of a problem as thanks to the highly configurable and extensible options provided by Spring Boot one could write their own Oracle UCP Connection Pool bean as shown in that particular blog post.

Some time after that post Spring 2.4.0 was released and with it came a host of new features, in particular official support for Oracle UCP! That's right, there's no longer the need to use your own custom Oracle UCP Connection Pool bean as you may use the one provided directly by Spring Boot. The benefits are clear: the bean is fully tested by the Spring Boot team; it provides the same developer experience as other connection pool beans, that is, all of its properties may be set using external configuration, there's content assist when writing configuration inside an IDE, and more.

To test it out we're going to have a look at the same example as the previous post but updated to match the most recent configuration provided by Spring Boot 2.4.5. To begin with, let's have a look at the pom.xml, the project's build file:

The only difference from the previous post is that we've updated Spring Boot and the Oracle dependencies to their latest versions as of May 2021, that'd be 2.4.5 and 21.1.0.0 accordingly. Next, the custom DataSourceConfig.java is no longer needed so we take it out. This leaves us with a basic Todo "domain" class (not using JPA but JdbcTemplate to keep it simple) and an Application class that performs basic queries. The domain class looks like this:

While the application class is nothing more than this:

To test out that the Oracle UCP configuration works we're going to configure the application with HikariCP, as this is the default provided by SpringBoot. We do this by defining the minimum set of properties that the DataSource requires in application.properties:

Now, HikariCP will be the selected option by SpringBoot because we did not specify a datasource type property. We can verify this by running the application which should result in output similar to this one:

We can appreciate that the Hikari Connection Pool was used in this run. Now let's switch things and configure Oracle UCP as Connection Pool. We do this by adding the following configuration properties to application.properties:

We can see now a property named spring.datasource.type that defines the type of DataSource to be used. This prompts SpringBoot to switch to Oracle UCP instead of HikariCP. There are additional properties that make sense for UCP as well. Note that if you edit this file on your IDE you'll get property name auto-completion, making it a snap to configure your application, neat! Running the application with this new configuration yields results similar to what's shown next:

Gone is the output from the HikariCP which means it's no longer in use. Instead, Oracle UCP kicks in and performs the work. And there you have it, working with Spring Boot and Oracle databases has become much simpler now that the framework provides integration from its core.

Credits
Photo by Jim Wellington

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.