Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:


1. Overview

Oracle is one of the most popular databases in large production environments. So, as Spring developers, it's very common to have to work with these databases.

In this tutorial, we're going to talk about how we can make this integration.

2. The Database

The first thing we need is, of course, the database. If we don't have one installed, we can get and install any of the databases available on the Oracle Database Software Downloads. But in case we don't want to do any installation, we can also build any of the Oracle database images for Docker.

In this case, we're going to use an Oracle Database 12c Release 2 ( Standard Edition Docker image. Consequently, this keeps us from having to install new software on our computer.

3. Connection Pooling

Now we have the database ready for incoming connections. Next, let's learn some different ways to do connection pooling in Spring.

3.1. HikariCP

The easiest way for connection pooling with Spring is using autoconfiguration. The spring-boot-starter-jdbc dependency includes HikariCP as the preferred pooling data source. Therefore, if we take a look into our pom.xml we'll see:


The spring-boot-starter-data-jpa dependency includes the spring-boot-starter-jdbc dependency transitively for us.

Now we only have to add our configuration into the file:

# OracleDB connection settings

# HikariCP settings

# JPA settings

As you can see, we have three different section configuration settings:

  • The OracleDB connection settings section is where we configured the JDBC connection properties as we always do
  • The HikariCP settings section is where we configure the HikariCP connection pooling. In case we need advanced configuration we should check the HikariCP configuration property list
  • The JPA settings section is some basic configuration for using Hibernate

That is all we need. It couldn't be easier, could it?

3.2. Tomcat and Commons DBCP2 Connection Pooling

Spring recommends HikariCP for its performance. On the other hand, it also supports Tomcat and Commons DBCP2 in Spring Boot autoconfigured applications.

It tries to use the HikariCP. If it isn't available, then tries to use the Tomcat pooling. If neither of those is available, then it tries to use Commons DBCP2.

We can also specify the connection pool to use. In that case, we just need to add a new property to our file:


If we need to configure specific settings, we have available their prefixes:

  • spring.datasource.hikari.* for HikariCP configuration
  • spring.datasource.tomcat.* for Tomcat pooling configuration
  • spring.datasource.dbcp2.* for Commons DBC2 configuration

And, actually, we can set spring.datasource.type to any other DataSource implementation. It isn't necessary to be any of the three mentioned above.

But in that case, we will just have a basic out-of-the-box configuration. There will be many cases where we will need some advanced configurations. Let's see some of them.

3.3. Oracle Universal Connection Pooling

If we want to use advanced configurations, we can declare the UCP datasource and set the remaining properties in the file. As of version of UCP, this is the easiest way of doing it.

Oracle Universal Connection Pool (UCP) for JDBC provides a full-featured implementation for caching JDBC connections. It reuses the connections instead of creating new ones. It also gives us a set of properties for customizing pool behavior.

If we want to use UCP, we need to add the following Maven dependencies:


Now we only have to add our configuration into the file:

# UCP settings
spring.datasource.ucp.sql-for-validate-connection=select * from dual 

In the above example, we've customized some pool properties:

  • spring.datasource.ucp.initial-pool-size specifies the number of available connections created after the pool is initiated
  • spring.datasource.ucp.min-pool-size specifies the minimum number of available and borrowed connections that our pool is maintaining, and
  • spring.datasource.ucp.max-pool-size specifies the maximum number of available and borrowed connections that our pool is maintaining

If we need to add more configuration properties, we should check the UCPDataSource JavaDoc or the developer's guide.

4. Older Oracle Versions

For versions prior to 11.2, like Oracle 9i or 10g, we should create an OracleDataSource instead of using Oracle's Universal Connection Pooling.

In our OracleDataSource instance, we turn on connection caching via setConnectionCachingEnabled:

public class OracleConfiguration {
    public DataSource dataSource() throws SQLException {
        OracleDataSource dataSource = new OracleDataSource();
        return dataSource;

In the above example, we were creating the OracleDataSource for connection pooling and configured some parameters. We can check all the configurable parameters on the OracleDataSource JavaDoc.

5. Conclusion

Nowadays, configuring Oracle database connection pooling using Spring is a piece of cake.

We've seen how to do it just using autoconfiguration and programmatically. Even though Spring recommends the use of HikariCP, other options are available. We should be careful and choose the right implementation for our current needs.

And, as always, the full example can be found over on GitHub.

Persistence bottom
Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE
Persistence footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!