转自 http://soft-development.iteye.com/blog/1401770
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;
public class PersonDefine_ConnectionPool implements Runnable {
public String ProductionDB = "axdb_tmp";//給的默認值
private String database_driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//給的默認值
// private String
// dakabase_url="jdbc:sybase:Tds:IP:port/dakabase_test?useUnicode=true&characterEncoding=UTF-8";
private String database_url = "jdbc:sqlserver://localhost:1433;DatabaseName=mgr_live"; //給的默認值
private String username = "sa";
private String password = "12345678";
private int maxConnections = 50;
private boolean waitIfBusy = true;
private Vector availableConnections, busyConnections;
private boolean connectionPending = false;
public PersonDefine_ConnectionPool(Properties props) throws SQLException {
System.out.println("jdbc:PersonDefine_ConnectionPool...");
int initialConnections = 0;
this.ProductionDB = props.getProperty("ELITEDATA.ProductionDB");
this.database_driver = props.getProperty("ELITEDATA.database_driver").trim();
this.database_url = props.getProperty("ELITEDATA.database_url");
this.username=props.getProperty("ELITEDATA.database_username");
this.password=props.getProperty("ELITEDATA.database_password");
this.maxConnections = maxConnections;
this.waitIfBusy = waitIfBusy;
if (initialConnections > maxConnections) {
initialConnections = maxConnections;
}
availableConnections = new Vector(initialConnections);
busyConnections = new Vector();
for (int i = 0; i < initialConnections; i++) {
availableConnections.addElement(makeNewConnection());
}
}
public synchronized Connection getConnection() throws SQLException {
System.out.println("jdbc:Get PersonDefine_ConnectionPool...");
if (!availableConnections.isEmpty()) {
Connection existingConnection = (Connection) availableConnections.lastElement();
int lastIndex = availableConnections.size() - 1;
availableConnections.removeElementAt(lastIndex);
// If connection on available list is closed (e.g.,
// it timed out), then remove it from available list
// and repeat the process of obtaining a connection.
// Also wake up threads that were waiting for a
// connection because maxConnection limit was reached.
if (existingConnection.isClosed()) {
notifyAll(); // Freed up a spot for anybody waiting
return (getConnection());
} else {
busyConnections.addElement(existingConnection);
return (existingConnection);
}
} else {
// Three possible cases:
// 1) You haven't reached maxConnections limit. So
// establish one in the background if there isn't
// already one pending, then wait for
// the next available connection (whether or not
// it was the newly established one).
// 2) You reached maxConnections limit and waitIfBusy
// flag is false. Throw SQLException in such a case.
// 3) You reached maxConnections limit and waitIfBusy
// flag is true. Then do the same thing as in second
// part of step 1: wait for next available connection.
if ((totalConnections() < maxConnections) && !connectionPending) {
makeBackgroundConnection();
} else if (!waitIfBusy) {
throw new SQLException("PersonDefine_ConnectionPool limit reached");
}
// Wait for either a new connection to be established
// (if you called makeBackgroundConnection) or for
// an existing connection to be freed up.
try {
wait();
} catch (InterruptedException ie) {
}
// Someone freed up a connection, so try again.
return (getConnection());
}
}
// You can't just make a new connection in the foreground
// when none are available, since this can take several
// seconds with a slow network connection. Instead,
// start a thread that establishes a new connection,
// then wait. You get woken up either when the new connection
// is established or if someone finishes with an existing
// connection.
private void makeBackgroundConnection() {
connectionPending = true;
System.out.println("jdbc:Make Background PersonDefine_ConnectionPool...");
try {
Thread connectThread = new Thread(this);
connectThread.start();
} catch (OutOfMemoryError oome) {
System.out.println("jdbc:PersonDefine_ConnectionPool out of Memory..." + oome);
}
}
public void run() {
try {
System.out.println("jdbc:run PersonDefine_ConnectionPool...");
Connection connection = makeNewConnection();
synchronized (this) {
availableConnections.addElement(connection);
connectionPending = false;
notifyAll();
}
} catch (Exception e) { // SQLException or OutOfMemory
System.out.println("jdbc:PersonDefine_ConnectionPool out of Memory...");
}
}
// This explicitly makes a new connection. Called in
// the foreground when initializing the ConnectionPool,
// and called in the background when running.
private Connection makeNewConnection() throws SQLException {
try {
System.out.println("jdbc:make new PersonDefine_ConnectionPool...");
// Load database dakabase_driver if not already loaded
Class.forName(database_driver);
// Establish network connection to database
Connection connection = DriverManager.getConnection(database_url, username, password);
return (connection);
} catch (ClassNotFoundException cnfe) {
// Simplify try/catch blocks of people using this by
// throwing only one exception type.
System.out.println("jdbc:PersonDefine_ConnectionPool Can't find class for database_driver:");
throw new SQLException("Can't find class for PersonDefine_ConnectionPool: " + database_driver);
}
}
public synchronized void free(Connection connection) {
System.out.println("jdbc:free PersonDefine_ConnectionPool...");
busyConnections.removeElement(connection);
availableConnections.addElement(connection);
// Wake up threads that are waiting for a connection
notifyAll();
}
public synchronized int totalConnections() {
return (availableConnections.size() + busyConnections.size());
}
/**
* Close all the connections. Use with caution: be sure no connections are
* in use before calling. Note that you are not <I>required</I> to call
* this when done with a ConnectionPool, since connections are guaranteed to
* be closed when garbage collected. But this method gives more control
* regarding when the connections are closed.
*/
public synchronized void closeAllConnections() {
System.out.println("jdbc:close all PersonDefine_ConnectionPool...");
closeConnections(availableConnections);
availableConnections = new Vector();
closeConnections(busyConnections);
busyConnections = new Vector();
}
private void closeConnections(Vector connections) {
try {
System.out.println("jdbc:close PersonDefine_ConnectionPool...");
for (int i = 0; i < connections.size(); i++) {
Connection connection = (Connection) connections.elementAt(i);
if (!connection.isClosed()) {
connection.close();
}
}
} catch (SQLException sqle) {
System.out.println("jdbc: PersonDefine_ConnectionPool..." + sqle);
}
}
public synchronized String toString() {
String info = "ConnectionPool(PersonDefine_ConnectionPool)(" + database_url + "," + username + ")" + ", available=" + availableConnections.size() + ", busy="
+ busyConnections.size() + ", max=" + maxConnections;
return (info);
}
}
相关推荐
数据库连接池是数据库管理中的重要概念,特别是在高并发和大数据量的应用场景下,它能显著提升性能并降低系统资源消耗。在C#编程环境中,我们可以使用自定义的数据库连接池来实现这一功能。本篇文章将深入探讨“C#...
对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接的配置,实现数据库连接池技术。某一应用最大可用数据库连接数的限制,避免某一应用独占所有数据库资源。 在较为完备的数据库连接池实现中,可根据...
数据库连接池是数据库管理中的一个重要概念,它在C#编程中扮演着优化数据库操作的关键角色。C#数据库连接池是一种管理数据库连接的技术,通过复用已存在的连接而不是每次请求时都创建新的连接,从而提高数据库操作的...
本资源集合了常用的JDBC数据库连接jar包,以及一些知名的数据库连接池实现,如dbcp和c3p0,这对于开发人员来说是非常宝贵的资源。 首先,让我们了解一下JDBC。JDBC提供了一套标准的API,包括接口和类,使得开发者...
数据库连接池是现代应用程序开发中的重要组成部分,尤其是在处理大量数据交互的应用中,它极大地提高了数据库操作的效率和系统的稳定性。本资源"03-数据库连接池驱动"包含了三种常用的数据库连接池驱动:C3P0、Druid...
本文将深入探讨如何在C#中使用MySQL数据库连接池。 首先,我们需要了解什么是数据库连接池。数据库连接池是一种资源管理技术,它预先创建并维护一定数量的数据库连接,当应用需要时,可以从池中获取连接,使用完毕...
Java JDBC 数据库连接池总结 Java 语言中,JDBC(Java DataBase Connection)是应用程序与数据库沟通的桥梁。在 Web 应用开发的早期,主要使用的技术是 CGIASPPHP 等。之后,Sun 公司推出了基于 Java 语言的 ...
* 数据库连接池特点: * 获取连接时不需要了解连接的名字,连接池内部维护连接的名字 * 支持多线程,保证获取到的连接一定是没有被其他线程正在使用 * 按需创建连接,可以创建多个连接,可以控制连接的数量 * 连接...
MySQL数据库连接池是提高应用程序性能的一种重要技术,它允许开发者管理多个数据库连接并高效地复用这些连接,而不是每次需要时都创建新的连接。在C#编程中,我们可以使用自定义的连接池或者第三方库如ADO.NET的...
数据库连接池在Java中的实现是提高应用程序性能的关键技术之一,它通过复用已存在的数据库连接,避免了频繁创建和销毁连接导致的系统资源浪费。本文将深入探讨如何使用Java代码来实现一个简单的数据库连接池,并解释...
在IT领域,数据库连接池是优化数据库访问性能的关键技术之一,尤其在高并发的应用场景下,合理配置数据库连接池能够显著提升系统响应速度并降低资源消耗。本文将深入解析几种常用的数据库连接池——Apache DBCP、C3P...
数据库连接池是数据库管理系统中的一个重要概念,主要用于优化数据库的连接操作。在C#编程中,数据库连接池可以高效地管理数据库连接,避免频繁创建和销毁连接导致的性能开销。本文将详细介绍C#中数据库连接池的工作...
数据库连接池是Java开发中非常重要的一个组件,它在处理多线程环境下对数据库资源的高效利用和管理中起着关键作用。C3P0是一个开源的Java连接池实现,它提供了一种灵活且功能强大的数据库连接管理方式。在本文中,...
数据库连接池是现代Java应用程序中不可或缺的组件,它在提高数据库操作效率和资源管理方面扮演着重要角色。Druid是一个高效、强大且功能丰富的数据库连接池实现,由阿里巴巴开源并维护。标题提到的"数据库连接池jar...
Delphi数据库连接池是一种高效的数据库资源管理技术,它允许应用程序在多用户环境下共享数据库连接,以提高性能并减少系统资源的消耗。连接池的核心思想是重用已建立的数据库连接,而不是每次需要时都创建新的连接,...
context.xml, 数据库连接池配置文
数据库连接池是现代应用程序中管理数据库连接的一种高效方式,它能显著提高系统性能,减少资源消耗。在给定的标题“配置数据库连接池”中,我们可以深入探讨数据库连接池的概念、工作原理,以及如何配置Oracle9i和...
### JAVA 使用数据库连接池连接Oracle数据库全代码解析 #### 一、概述 本文将详细介绍如何在Java项目中使用Apache DBCP(Database Connection Pool)来连接Oracle数据库,并提供完整的示例代码。通过这种方式,我们...