1. Overview

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

In this tutorial, we will 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 install any databases available on the Oracle Database Software Downloads. But if we don’t want to do any installation, we can also build any Oracle database images for Docker.

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

3. Connection Pooling

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

3.1. HikariCP

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


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 application.properties 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 try to use the Tomcat pooling. If neither of those is available, it tries to use Commons DBCP2.

We can also specify the connection pool to use. In that case, we need to add a new property to our application.properties 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 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 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 application.properties file. Currently, the best way to enable UCP in a spring-boot application is through the Oracle-provided spring-boot-starter library.

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 behaviour.

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

   <version>3.1.0</version> <!-- 2.7.7 for Spring Boot 2.x -->

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

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

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

  • spring.datasource.oracleucp.initial-pool-size specifies the number of available connections created after the pool is initiated
  • spring.datasource.oracleucp.min-pool-size specifies the minimum number of available and borrowed connections that our pool is maintaining, and
  • spring.datasource.oracleucp.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 before 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 created the OracleDataSource for connection pooling and configuring 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 correct implementation for our current needs.

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

Course – LSD (cat=Persistence)

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

res – Persistence (eBook) (cat=Persistence)
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.