我自己实现的数据库连接池
包含的功能:
1、容纳n个打开的连接
2、容纳最大m 最大连接数
2、确定某个连接什么时间在使用
3、如果请求使用n+1个连接,它能创建一个新的连接对象,这个对象包含它的存活时间,过期,则移除该对象
4、当连接超过最大连接数m , 则必须等待,直到有空闲的连接 才可以使用
主要类的代码:
package database.pool;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
import java.util.Vector;
public class DBConnectFactory {
private static DBConnectFactory factory = null;
private static final int INIT_SIZE = 1;
private static final int MAX_SIZE = 2;
private long activeTime = 20000;
private Vector<DBConnection> connectPool = null;
private String driver;
private String url;
private String username;
private String password;
private DBConnectFactory() {
/*
* 构造方法,初始化
*
*/
try {
validate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
initProperties();
this.initPool();
}
public static synchronized DBConnectFactory getDBConnectFactory() {
if (null == factory) {
factory = new DBConnectFactory();
}
return factory;
}
private void validate() throws Exception {
/*
*
* 简单的验证
*
*/
if (INIT_SIZE < 0 || MAX_SIZE < 0) {
throw new Exception("连接池的数据为负数,不可以执行!");
}
if (INIT_SIZE > MAX_SIZE) {
throw new Exception("数据库最大连接数小于最小连接数!");
}
if(activeTime < 0){
throw new Exception("连接池的生命周期不可以为负数!");
}
}
private void initProperties() {
/*
*
* 初始化连接的配置
*
*/
Properties dbPro = new Properties();
InputStream input = this.getClass()
.getResourceAsStream("db.properties");
try {
dbPro.load(input);
this.driver = dbPro.getProperty("driver");
this.url = dbPro.getProperty("url");
this.username = dbPro.getProperty("username");
this.password = dbPro.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initPool() {
/*
* 初始化数据库连接池
*
*/
if (null == connectPool) {
connectPool = new Vector<DBConnection>(INIT_SIZE);
System.out.println(connectPool.size());
for (int i = 0; i < INIT_SIZE; i++) {
DBConnection db = new DBConnection(driver, url, username,
password);
connectPool.add(db);
}
}
}
private DBConnection createNewConectionTimer() {
/*
* 创建一个连接(新增加连接): 不过,这个连接对象是 DBConnectionTimer
*
*/
synchronized (connectPool) {
DBConnection db = new DBConnectionTimer(driver, url, username,
password, activeTime);
connectPool.add(db);
return db;
}
}
public Connection getConnection() {
/*
*
* 返回一个数据库连接对象
*/
synchronized (connectPool) {
DBConnection db = null;
Connection con = null;
while (true) { //如果没有找到连接,则必须找到连接才可以退出循环!!!
for (int i = 0; i < connectPool.size(); i++) {
db = connectPool.get(i);
if (!db.isUse()) {
if (db instanceof DBConnectionTimer) { // 取消定时器
DBConnectionTimer dbTimer = (DBConnectionTimer) db;
dbTimer.cacel();
con = db.getCon();
db.setUse(true); // 设置使用该连接
return con; // 直接返回
} else {
con = db.getCon();
db.setUse(true); // 设置使用该连接
return con; // 直接返回
}
}
}
if (null == con && connectPool.size() < MAX_SIZE) {
/*
* 连接池的对象没有达到最大值。
*/
System.out.println("创建新的连接对象");
db = this.createNewConectionTimer(); // 创建新连接
con = db.getCon();
db.setUse(true); // 新连接设置为使用
return con;
}
if (null == con && connectPool.size() == MAX_SIZE) {
// 已经达到最大连接,而且没有使用的连接对象。 应该是连接等待.
/*
* 差点在这里出错了, 应该在这里使用一个死循环,直到找到一个连接对象
*
*/
try {
connectPool.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void releaseConnection(Connection con) {
/*
* 释放连接 标示连接对象可以再次使用!
*
*/
synchronized (connectPool) {
for (int i = 0; i < connectPool.size(); i++) {
DBConnection db = connectPool.get(i);
if (db instanceof DBConnectionTimer) {
// 计时器开始计时
DBConnectionTimer dbTimer = (DBConnectionTimer) db;
DBConTimerTask task = new DBConTimerTask(connectPool,
dbTimer);
dbTimer.tick(task);
} else {
// 固定的连接,一直存在。
if (con == db.getCon()) { // 寻找连接,并判断是否相等;
db.setUse(false);
connectPool.notify(); // 通知,可以使用连接对象了
break; // 结束循环
}
}
}
}
}
}