日期:2014-05-20  浏览次数:20890 次

我做c/s啊想用连接池啊!!!!!!!!!!!!但是没找到相关资料啊
我用

cs   java   连接池

baidu  
google都没有找到相关的资料啊

555

------解决方案--------------------
直接找个开源的不得了?


------解决方案--------------------
通常商业数据库JDBC驱动都会提供自已的数据库连接池管理器,无需自己制做。
无论如何,这里给出一个简单实现

实现连接池的关键点:
1. 单实例
2. 由实现类来管理数据库连接,包括连接,刷新
3. 重载java.sql.Connection的close功能。当客户程序调用close时不是真正的断开连接,而是将连接归还连接池管理器。
4. 将数据库属性独立于管理器本身

可选:
1. 实现javax.sql.PooledConnection
2. 实现javax.sql.DataSource
3. 实现javax.sql.ConnectionPoolDataSource

一个简单实现:
import java.io.*;
import java.util.*;
import java.sql.*;

public class ConnectionPool implements Runnable {
static String configFileName; // 如./connection-pool.properties
static Properties config;
private static List list = null;
private static boolean isCreate = false;
/**
* 实现单实例,关键点1
* @throws SQLException
*/
public ConnectionPool () throws SQLException {
if (list == null) init ();
if (!isCreate) {
Thread thread = new Thread(this);
thread.run();
isCreate = true;
}
}

public synchronized Connection getConnection () {
if (list == null) return null;

// 寻找空闲的连接
for (int i = 0; i < list.size(); i ++){
ConnectionWrap conn = (ConnectionWrap)list.get(i);
if (conn.free) {
conn.free = false;
conn.start = System.currentTimeMillis();
return conn;
}
}

// 没有空闲的连接则自动扩展
int step = Integer.parseInt(config.getProperty( "step ")); // 取得自动扩展的步长
this.addConnection(step);
return (ConnectionWrap)list.get(list.size() - step - 1);
}

public void run () {
long interval = Long.parseLong(config.getProperty( "interval ")); // 扫描时间
while (true) {
try {
Thread.sleep(interval);
} catch (Exception e) {
// 出错处理
}
scan ();
}
}

/**
* 关键点2,管理连接。一些超时的连接刷新
*/
private void scan () {
int timeout = Integer.parseInt(config.getProperty( "timeout "));
for (int i = 0; i < list.size(); i ++) {
ConnectionWrap conn = (ConnectionWrap) list.get(i);
if (conn.start > ; 0) {
time = System.currentTimeMillis() - conn.start;
if (time > ;= timeout)
conn.close();
}
}

int initCount = Integer.parseInt(config.getProperty( "init-count "));
if (list.size() > ; initCount) { // 恢复到初始连接数
for (int i = list.size() - 1; i > ; initCount; i --) {
ConnectionWrap conn = (ConnectionWrap) list.get(i);
if (conn.free) { // 真正地断开连接
try {
conn.conn.close();
} catch (SQLException ignore){}
list.remove(conn);
}
}
}
}

private void init () throws SQLException {
config = readConfig ();
createConnectionPool (config);
}

/**