这是我以前写的一段程序,试图实现数据库链接池的功能。运行时也还正常,就是感觉程序太简单,不知道数据库连接池是不是按这个思路写的。程序不长,请热心肠的大侠们评价一下,小弟先谢了!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();
}
}
}
解决方案 »
- 大哥们帮忙解决一个截取字符串的问题!
- java.awt 与 java.swing 有什么区别啊?谢谢。
- java下读写xml用什么方便?
- 请教一个小问题..
- 如何用JAVA实现WINDOWS 下的MS-DOS命令
- 读大文本数据中的问题
- 谁能告诉我win32comm.dll哪里能下载到啊?
- 用System.in.read()可以执行的程序用System.in.readline()就不能执行
- 如何将Oracle数据库转换成Sql server 数据库
- javabean+jsp+配置文件,如何实现类似IE的“整理我的收藏夹”页面,请高手援助!
- 一个关于Vector的程序,不知道为什么出错
- 求解Public static与Private static的区别
可以参考一下tomcat的连接池的代码
1)共享的连接如何确定是否已经被数据库端close?
2)并发取连接的时候如何处理?
3)怎么控制连接满时的动态增长?
答:这个确实没有考虑过。1)共享的连接如何确定是否已经被数据库端close?
答:Connection有isClosed()方法,我是用这个判断的。2)并发取连接的时候如何处理?
答:取连接和返回连接的方法都加了synchronized,不知这样可不可以。3)怎么控制连接满时的动态增长?
答:感谢liu_you(滴水藏海) 提出这个问题。这个我没有实现,现在连接满时就只有等待,确实是个问题。