Can I have Spring use getPooledConnection instead getConnection from the Datasource when using Oracle?

I have the following code in a simple spring boot app…

@Bean
public DataSource getDatasource(){
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName(driver);
    ds.setUsername(username);
    ds.setUrl(url);
    ds.setPassword(password);
    return ds;
}

This worked great but I wanted connection pooling so I changed to…

@Bean
public DataSource getDatasource(){
    try{
        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
        ds.setDriverType("thin");
        ds.setUser(username);
        ds.setNetworkProtocol("tcp");
        ds.setPassword(password);
        ds.setDatabaseName(dbName);
        ds.setServerName(serverName);
        return ds;
    } catch (SQLException throwables) {
        logger.error(throwables);
        System.exit(-1);
    }
    return null;
}

But from the documentation it looks like getConnection just returns a native connection and I need to configure Spring to call getPooledConnection instead.

Is there another bean I can create or some other way I can do this?

Answer

It looks like if you are doing this with Oracle you are supposed to use a library called UCP or Universal Connection Pooling…

<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ucp</artifactId>
    <version>21.1.0.0</version>
</dependency>

Then I use that to create the datasource like…

PoolDataSource ds = PoolDataSourceFactory.getPoolDataSource();
ds.setConnectionFactoryClassName(OracleDataSource.class.getName());
ds.setURL(url);
ds.setUser(username);
ds.setPassword(password);
ds.setInitialPoolSize(1);
ds.setMinPoolSize(1);
ds.setMaxPoolSize(10);
return ds;

I am finishing my testing to make sure it is working as expected.