这是我以前写的一段程序,试图实现数据库链接池的功能。运行时也还正常,就是感觉程序太简单,不知道数据库连接池是不是按这个思路写的。程序不长,请热心肠的大侠们评价一下,小弟先谢了!package org.eleaf.java.eshop.db;import java.sql.*;
import java.util.*;public class ConnectionPool {
private final int MAX_CONNS; //最大连接数
private Vector freeConns; //当前可用空闲连接集合
private int activeConns; //当前活动连接
private Database db; //包含JDBC驱动、URL、用户名、密码等基本信息并生成数据库连接的一个类。
private static ConnectionPool instance;
        //单例模式
public static synchronized ConnectionPool getInstance() throws SQLException {
if (instance == null) {
instance = new ConnectionPool();
}
return instance;
}
public ConnectionPool() throws SQLException {
db = Database.getInstance();
freeConns = new Vector();
MAX_CONNS = db.getMaxConnections();
activeConns = 0;
showInfo("ConnectionPool()");
}
        //创建新连接
private Connection createNewConnection() throws SQLException {
showInfo("createNewConnection()");
return db.getConnection();
}
        //从连接池中取得连接
public synchronized Connection getConnection() throws SQLException {
while (activeConns >= MAX_CONNS) { //如果活动连接数超过了最大允许数,则等待1妙钟。
try {
wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Connection conn = null;
if (freeConns.size() <= 0) { //如果当前可用空闲连接为0,则创建新连接
conn = createNewConnection();
activeConns++;
} else { //如果当前可用空闲连接不为0,则从池中取出第一个空闲连接。
conn = (Connection) freeConns.get(0);
freeConns.remove(0);
if (conn == null) {
System.out.println("_______________");
conn = createNewConnection();
}
activeConns++;
}
showInfo("getConnection()");
return conn;
}
        //线程程序执行完毕,要主动返回连接到连接池中。
public synchronized void returnConnection(Connection conn) throws SQLException {
if (conn == null || conn.isClosed()) {
return;
}
if (activeConns > MAX_CONNS) {
conn.close();
conn = null;
} else {
freeConns.addElement(conn);
}
activeConns--;
showInfo("returnConnection(Connection)");
notify();//连接返回到池中后,通告其它等待取得连接的线程。
}
private void showInfo(String str) {
String t = Thread.currentThread().getName();
String tmp = t + " : " + str + " : activeConns=" + activeConns +
", freeConns=" + freeConns.size();  
System.out.println(tmp);
}
        //一段测试程序
public static void main(String[] args) {
System.out.println("*************");
final ConnectionPool cp = ConnectionPool.getInstance();
Thread[] ths = new Thread[100];
for (int i = 0; i < ths.length; i++) {
ths[i] = new Thread(
new Runnable() {
public void run() {
Connection conn = cp.getConnection();
try {
cp.showInfo("start business...");
Thread.sleep(500);
cp.showInfo("end business...");
} catch (InterruptedException e) {
e.printStackTrace();
}
cp.returnConnection(conn);
}
}, "T" + (i + 1)
);
}
for (int i = 0; i < ths.length; i++) {
ths[i].start();
}
}
}

解决方案 »

  1.   

    有点长,不过对于连接池来说似乎太短了,
    可以参考一下tomcat的连接池的代码
      

  2.   

    就共享连接的思路已经实现了,提几点:
    1)共享的连接如何确定是否已经被数据库端close?
    2)并发取连接的时候如何处理?
    3)怎么控制连接满时的动态增长?
      

  3.   

    谢谢上面几位大侠,主要是想在找工作面试时能拿出来加分,不知道这个目的能不能达到?回答:不知道要是连接超时了怎么办?
    答:这个确实没有考虑过。1)共享的连接如何确定是否已经被数据库端close?
    答:Connection有isClosed()方法,我是用这个判断的。2)并发取连接的时候如何处理?
    答:取连接和返回连接的方法都加了synchronized,不知这样可不可以。3)怎么控制连接满时的动态增长?
    答:感谢liu_you(滴水藏海) 提出这个问题。这个我没有实现,现在连接满时就只有等待,确实是个问题。