栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 中的连接池示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 中的连接池示例

在此页面上,您将学习如何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 依赖项。


    commons-dbcp
    commons-dbcp
    1.4

什么是连接池?

连接池是一种创建和维护 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()– 检查借用的连接数。

检查完整的例子。

OracleUCP.java
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)- 以毫秒为单位设置最大等待时间。

检查完整的例子。

ApacheDBCP.java
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();
		}

	}
}
复制 参考
  1. 高性能 Oracle JDBC 编程
  2. BasicDataSource(公共 DBCP 1.4 API)
  3. DBCP 组件
  4. 连接池
  5. Tomcat JDBC 连接池

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号