Connection Pooling: How to Tune Oracle UCP

Connection pool examples usually start with pool sizing and end with a happy-path query. If you’re building a production-grade service with Oracle UCP (Universal Connection Pool), that might miss the point: Your connection pool includes a vast array of features, covering failover, database resident connection pooling, caching, and more.

In this article, we’ll walk through how to configure several common Oracle UCP scenarios in Spring Boot. Download the code sample here, and if you like it, star the repo!

UCP scenarios

UCP Scenarios are configured through application properties, code, or both.

ScenarioSpring Boot ProfileWhat It Does
UCP defaultdefaultReplaces the default Hikari pool with Oracle UCP.
Pool sizingsizingDemonstrates standard connection pool sizing.
Runtime resizingdynamic-resizingAdjusts connection pool size at runtime
Static sizingstatic-sizingPre-warmed pool useful to avoid connection storms
Timeout cleanuptimeoutsControls stale connections in idle, reused, and exhausted pools.
Abandoned connection cleanupabandonedPrevent code paths that may leak, block, or hold connections.
Connection harvestingharvestingMark selected borrowed connections as reclaimable when pool is exhausted.
Statement cachingstatement-cacheReduce query parse overhead from repeated SQL.
Borrow validationvalidationStale connection protection without validating every recently used connection.
Connection labelingcode-backed exampleWorkloads that need reusable connections with known session state.
DRCPdrcpClient-side pool for Database
Resident Connection Pooling
.

The sample also makes the connection inspectable through the PoolMetrics, PoolDiagnostics, and PoolReport classes. These classes report bound settings, live counts, and diagnostics for test verification.

Oracle UCP pool inspection

Some UCP features need code, not just properties

Most profiles in the sample are configuration-only. Two examples intentionally cross into application code.

HarvestingService borrows a connection, casts it to HarvestableConnection, marks it non-harvestable while work is active, and marks it harvestable again in a finally block.

HarvestableConnection harvestableConnection = (HarvestableConnection) connection;
harvestableConnection.setConnectionHarvestable(false);
try {
    // use the connection for work
} finally {
    harvestableConnection.setConnectionHarvestable(true);
}

ConnectionLabelingService registers a ConnectionLabelingCallback, requests a connection with a transaction isolation label, and configures the JDBC connection state when the current labels do not match.

Database Resident Connection Pooling: DRCP

DRCP is a server-side connection pool that many clients can share, allowing one or more apps to use the same connection pool. DRCP allows the connection pool for a group of applications to be managed server-side.

The drcp profile demonstrates client-side UCP settings for Oracle AI Database Resident Connection Pooling. It uses the short pooled URL form to connect:

jdbc:oracle:thin:@localhost:1521/freepdb1:POOLED

It also sets a DRCP connection class:

spring:
  datasource:
    oracleucp:
      connection-properties:
        oracle.jdbc.DRCPConnectionClass: spring-boot-ucp

DRCP can be enabled server-side with PL/SQL. For example, configuring a pool:

begin
    dbms_connection_pool.configure_pool(
        pool_name => 'SYS_DEFAULT_CONNECTION_POOL',
        minsize => 1,
        maxsize => 4,
        incrsize => 1,
        session_cached_cursors => 20
    );
    dbms_connection_pool.start_pool();
end;

Take each configuration for a spin

Runnable examples are always more fun. SpringBootUcpTest starts Oracle AI Database Free with Testcontainers:

@Container
@ServiceConnection
static final OracleContainer oracle = new OracleContainer("gvenzl/oracle-free:23.26.2-slim-faststart")
        .withStartupTimeout(Duration.ofMinutes(5))
        .withUsername(APP_USERNAME)
        .withPassword(APP_PASSWORD);

The tests unwrap the configured DataSource as a UCP PoolDataSource, run select 1 from dual, and assert that each profile binds the expected values, for example:

  • The harvesting test verifies that a connection can be marked non-harvestable while work runs.
  • The labeling test verifies the requested transaction isolation label, the JDBC isolation level, and that no labels are left unmatched.
  • The DRCP test starts a server side connection pool with dbms_connection_pool, boots the drcp profile, checks the configured connection class, and runs the smoke query through the pooled server URL.

Run the module test:

mvn -pl spring-boot-ucp test

When to use Oracle UCP vs. Hikari CP

Hikari is highly optimized for rapidly acquiring connections and releasing them. If this is your primary connection pool use case, adopt Hikari in your application. It’s widely used and quite performant for these types of simple use cases.

UCP shines when you need advanced, vendor-specific features of Oracle AI Database. I’d configure UCP for any application that needs failover, DRCP, or one of the other techniques described here.

How to get started with Oracle UCP

Include UCP as a spring boot starter:

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

Set the data source type explicitly:

spring:
  application:
    name: spring-boot-ucp
  datasource:
    username: ${USERNAME:testuser}
    password: ${PASSWORD:testpwd}
    url: ${JDBC_URL:jdbc:oracle:thin:@localhost:1521/freepdb1}

    # Set these to use UCP over Hikari.
    driver-class-name: oracle.jdbc.OracleDriver
    type: oracle.ucp.jdbc.PoolDataSource

Then, begin tuning the pool. The sample gives you starting points for sizing, timeouts, connection harvesting, caching, drcp, and more.

For additional configuration options, refer to the UCP Developer’s guide. If you have questions, let me know!

Important Files

  • SpringBootUcpTest: Verifies UCP samples with Oracle AI Database Free.
  • application.yaml: Contains each profile-specific UCP configuration.
  • PoolMetrics: Captures runtime UCP pool statistics such as total, borrowed, available, pending, wait, and cumulative borrow/return counts.
  • PoolDiagnostics: Captures UCP diagnostic settings, JMX status, metric update interval, and registered pool names.

References

Leave a Reply