Oracle 通用连接池 (UCP) 是一个功能丰富的连接池,它提供与 Oracle Real Application Clusters (RAC)、Active Data Guard (ADG)、Global Data Services (GDS) 的无缝集成,为高可用性提供内置支持,可扩展性和性能特点。对于旧版本的 Spring Boot,需要一个额外的 bean 类来使用 UCP 作为数据源。现在,使用 SpringBoot v2.4.0 及更高版本,无需任何额外代码即可更轻松地配置 UCP。Spring 将 UCP 标识为数据源,就像application.properties 中的任何其他数据源一样。按照以下步骤查看 UCP 的运行情况。
#1:从 Github下载示例Spring 应用程序。
#2:确保您使用的是最新的 Spring-Boot 版本。在pom.xml 中:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
#3:在application.properties 中指定 UCP 作为数据源。您可以根据需要设置其他UCP 属性。确保更新数据库 URL、用户名和密码以指向您的数据库。
# For connecting to Autonomous Database (ATP) refer https://www.oracle.com/database/technologies/getting-started-using-jdbc.html
# Provide the database URL, database username and database password
spring.datasource.url=jdbc:oracle:thin:@dbname_alias?TNS_ADMIN=/Users/test/wallet/wallet_dbname_alias
spring.datasource.username=<your-db-user>
spring.datasource.password=<your-db-password>
# Properties for using Universal Connection Pool (UCP)
# Note: These properties require JDBC version 21.0.0.0
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.type=oracle.ucp.jdbc.PoolDataSource
# For using Replay datasource
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.replay.OracleDataSourceImpl
# For using Non-Replay datasource
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource
spring.datasource.oracleucp.sql-for-validate-connection=select * from dual
spring.datasource.oracleucp.connection-pool-name=connectionPoolName1
spring.datasource.oracleucp.initial-pool-size=15
spring.datasource.oracleucp.min-pool-size=10
spring.datasource.oracleucp.max-pool-size=30
#4:编译并运行OracldJdbcApplication.java以验证与数据库的连接。
#5:使用JDBCSampleData.java创建一个新用户 testuser以及 EMP 和 DEPT 表,并测试列出所有员工和插入新员工的其他功能。