From e03662dd54b2ebf35b93c9a5f5277b44ea39fcdf Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Sun, 19 Sep 2021 17:56:49 -0400 Subject: [PATCH] JAVA-7334: Update 'Oracle Connection Pooling With Spring' article --- .../spring-boot-persistence-2/pom.xml | 2 +- .../configuration/OracleConfiguration.java | 3 +- .../configuration/OracleUCPConfiguration.java | 32 ------------------- ...pplication-oracle-pooling-basic.properties | 9 ++++++ ...lePoolingApplicationOracleUCPLiveTest.java | 6 ++-- 5 files changed, 16 insertions(+), 36 deletions(-) delete mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java diff --git a/persistence-modules/spring-boot-persistence-2/pom.xml b/persistence-modules/spring-boot-persistence-2/pom.xml index b51ab17659..1a02b18e50 100644 --- a/persistence-modules/spring-boot-persistence-2/pom.xml +++ b/persistence-modules/spring-boot-persistence-2/pom.xml @@ -146,7 +146,7 @@ 3.9.1 2.1.8.RELEASE 0.9.5.2 - 19.6.0.0 + 21.1.0.0 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java index 9cf7e27c99..e201c9eb76 100644 --- a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java @@ -20,7 +20,8 @@ public class OracleConfiguration { dataSource.setUser("books"); dataSource.setPassword("books"); dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1"); - dataSource.setFastConnectionFailoverEnabled(true); + // Only with clients prior to v21 + // dataSource.setFastConnectionFailoverEnabled(true); dataSource.setImplicitCachingEnabled(true); // Only with clients prior to v11.2 // dataSource.setConnectionCachingEnabled(true); diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java deleted file mode 100644 index b4c1544149..0000000000 --- a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.oracle.pooling.configuration; - -import java.sql.SQLException; - -import javax.sql.DataSource; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; - -import oracle.ucp.jdbc.PoolDataSource; -import oracle.ucp.jdbc.PoolDataSourceFactory; - -@Configuration -@Profile("oracle-ucp") -public class OracleUCPConfiguration { - - @Bean - public DataSource dataSource() throws SQLException { - PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource(); - dataSource.setUser("books"); - dataSource.setPassword("books"); - dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); - dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1"); - - dataSource.setFastConnectionFailoverEnabled(true); - dataSource.setInitialPoolSize(5); - dataSource.setMinPoolSize(5); - dataSource.setMaxPoolSize(10); - return dataSource; - } -} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties b/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties index 9a1c7fc89d..9de2ee476b 100644 --- a/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/application-oracle-pooling-basic.properties @@ -21,6 +21,15 @@ spring.datasource.hikari.poolName=HikariPoolBooks spring.datasource.tomcat.maxActive=15 spring.datasource.tomcat.minIdle=5 +# UCP settings +#Note: These properties require JDBC version 21.0.0.0 +spring.datasource.ucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource +spring.datasource.ucp.sql-for-validate-connection=select * from dual +spring.datasource.ucp.connection-pool-name=UcpPoolBooks +spring.datasource.ucp.initial-pool-size=5 +spring.datasource.ucp.min-pool-size=5 +spring.datasource.ucp.max-pool-size=10 + # JPA settings spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect spring.jpa.hibernate.use-new-id-generator-mappings=false diff --git a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java index 4fb6aa6bae..cf418b2cf4 100644 --- a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java +++ b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java @@ -9,11 +9,13 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = {SpringOraclePoolingApplication.class}) -@ActiveProfiles({"oracle-pooling-basic", "oracle-ucp"}) +@ActiveProfiles({"oracle-pooling-basic"}) +@TestPropertySource(properties = "spring.datasource.type=oracle.ucp.jdbc.UCPDataSource") public class SpringOraclePoolingApplicationOracleUCPLiveTest { @Autowired @@ -21,7 +23,7 @@ public class SpringOraclePoolingApplicationOracleUCPLiveTest { @Test public void givenOracleUCPConfiguration_thenBuildsOraclePoolDataSource() { - assertTrue(dataSource instanceof oracle.ucp.jdbc.PoolDataSource); + assertTrue(dataSource instanceof oracle.ucp.jdbc.UCPDataSource); } }