0

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?

JGleason
  • 3,067
  • 6
  • 20
  • 54
  • Your configuration should ideally use the OracleConnectionPoolDataSource. Can you share the full configuration? What are you using for persistence? ORM or plain JDBC? – bluelurker Mar 24 '21 at 14:50
  • 1
    Does [this](https://stackoverflow.com/questions/12660724/oracle-connection-pool-class) help? – crizzis Mar 24 '21 at 15:05
  • @bluelurker I think I am using the Pool Datasource in the second one rt? The problem isn't that it is getting the connection from the ds. Which it seems the Oracle Pooled source provides a different method name (getPooledConnection) instead of getConnection. getConnection is provided but I don't think it uses the pool. – JGleason Mar 24 '21 at 15:06
  • @crizzis might let me try it out – JGleason Mar 24 '21 at 15:11
  • You do not have to worry about the method names when using connection pools. All you have to do is create a bean of a `DataSource` implementation. Check this https://www.baeldung.com/spring-oracle-connection-pooling#older-oracle-versions – bluelurker Mar 24 '21 at 15:11
  • @crizzis that helped thank you but since I think it is a diff question I am not going to delete for being duplicate. – JGleason Mar 24 '21 at 15:47

2 Answers2

0

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.

JGleason
  • 3,067
  • 6
  • 20
  • 54
0

For using Universal Connection Pool (UCP), you do need ucp.jar in the classpath. Refer to UCPSample.java for an example.

Nirmala
  • 1,278
  • 1
  • 10
  • 11