在此页面上,您将学习如何JDBC
使用 Java 编程语言创建连接池。为了在我们的应用程序中创建连接池,Sun Microsystem 提供了一个接口 DataSource
通过使用接口 DataSource
,开发了许多第三方 API。例如。
1- Apache 已开发 BasicDataSource。
2- Mchange-cp30 供应商开发ComboPooledDataSource
。
3- Oracle WebLogic 已提供WebLogicDataSource
。
在我的示例中,我使用了 Oracle通用连接池和 Apache DBCP API。您可以从以下链接下载这些 jar 文件。
ucp.jar —————————————————点击这里
dbcp.jar ———————————————-点击这里
如果您使用 Maven,还可以在pom.xml文件中添加 DBCP 依赖项。
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
什么是连接池?
连接池是一种创建和维护 JDBC 连接对象的机制。
连接池用于增强应用程序的性能并在数据库上执行命令。
仅当没有可重用的连接对象时,才会创建新的连接对象。
这种技术可以提高应用程序的整体性能。
使用 Oracle UCP
本示例将帮助您使用 oracle 通用连接 API 创建连接池。
连接属性
设置数据源的连接属性。
dataSource.setConnectionFactoryClassName("oracle.jdbc.OracleDriver");
dataSource.setURL("jdbc:oracle:thin:@localhost:1521:xe");
dataSource.setUser("user_name");
dataSource.setPassword("password");
设置池属性
dataSource.setInitialPoolSize(10)
– 池管理器将使用 10 个物理连接启动。
dataSource.setMinPoolSize(20)
– 池维护线程将确保有 20 个物理连接可用。
dataSource.setMaxPoolSize(50)
– 池维护线程将检查可用的物理连接不超过 50 个。
dataSource.setPropertyCycle(20)
– 池维护线程将每 20 秒唤醒并检查一次池。
dataSource.setMaxIdleTime(300)
– 池维护线程将删除超过 300 秒不活动的物理连接。
dataSource.getAvailableConnectionsCount()
– 检查可用连接的数量。
dataSource.getBorrowedConnectionsCount()
– 检查借用的连接数。
检查完整的例子。
package org.websparrow.connection;
import java.sql.Connection;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
public class OracleUCP {
public static void main(String[] args) {
try {
PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
// dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
dataSource.setConnectionFactoryClassName("oracle.jdbc.OracleDriver");
dataSource.setURL("jdbc:oracle:thin:@localhost:1521:xe");
dataSource.setUser("user_name");
dataSource.setPassword("password");
dataSource.setInitialPoolSize(10);
dataSource.setMinPoolSize(20);
dataSource.setMaxPoolSize(50);
dataSource.setPropertyCycle(20);
dataSource.setMaxIdleTime(300);
for (int i = 1; i <= 1000; i++) {
Connection conn = dataSource.getConnection();
System.out.println(conn + " : " + i);
int avlConnCount = dataSource.getAvailableConnectionsCount();
System.out.println("Available connections: " + avlConnCount);
int brwConnCount = dataSource.getBorrowedConnectionsCount();
System.out.println("Borrowed connections: " + brwConnCount);
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用 Apache DBCP
ApacheBasicDataSource
类实现了DataSource
实现连接池的接口。
创建BasicDataSource
类的对象。
BasicDataSource bds = new BasicDataSource();
连接属性
设置数据源的连接属性。
bds.setDriverClassName("oracle.jdbc.OracleDriver");
bds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
bds.setUsername("user_name");
bds.setPassword("password");
设置池属性
bds.setMaxActive(10)
- 设置可以同时分配的最大活动连接数。
bds.setMaxIdle(5)
- 设置池中可以保持空闲的最大连接数。
bds.setMaxWait(1000 * 5)
- 以毫秒为单位设置最大等待时间。
检查完整的例子。
package org.websparrow.connection;
import java.sql.Connection;
import org.apache.commons.dbcp.BasicDataSource;
public class ApacheDBCP {
public static void main(String[] args) {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("oracle.jdbc.OracleDriver");
bds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
bds.setUsername("user_name");
bds.setPassword("password");
bds.setMaxActive(10);
bds.setMaxIdle(5);
bds.setMaxWait(1000 * 5);
try {
for (int i = 1; i <= 200; i++) {
Connection conn = bds.getConnection();
System.out.println(conn + " : " + i);
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}