一个连接容器,记录连接和连接使用状况
package db.khan;
import java.sql.*;
/**//*数据库连接容器
* */
public class DBPoolCon {
/**//*容器中的连接*/
public Connection con = null;
public int nUsedTimes = 0;
DBPoolCon() {
}
public Connection get() {
return con;
}
}
一个操作和管理连接容器的连接池
package db.khan;
import java.sql.*;
import java.util.*;
/** *//**连接池类
* */
public class DBConPool {
protected LinkedList listCon = new LinkedList();// 一个存放链接池的链表
protected String DBUrl = null;
protected String DBUser = null;
protected String DBPasswd = null;
/** *//**最大连接数*/
protected int nMaxCon = 0;
/** *//**连接最大使用时间*/
protected int nMaxUsedTime = 0;
/** *//**当前已用连接数*/
protected int nConNum = 0;
/** *//**构造器
* @param String DBUrl
* @param String DBUser
* @param String DBPasswd
* @param int nMaxCon
* @param int nMaxUsedTime*/
DBConPool(String DBUrl, String DBUser, String DBPasswd, int nMaxCon, int nMaxUsedTime) {
this.DBUrl = DBUrl;
this.DBUser = DBUser;
this.DBPasswd = DBPasswd;
this.nMaxCon = nMaxCon;
this.nMaxUsedTime = nMaxUsedTime;
}
/** *//**从连接池中取得连接(线程安全函数)
* @return DBPoolCon 返回的数据库连接容器
* */
synchronized public DBPoolCon get() {
if (listCon.size() > 0) {//有连接时,直接取得连接
return (DBPoolCon) listCon.removeFirst();
}
if (nConNum < nMaxCon) {//如果没有多余连接,但连接池未满,新建一个连接
return createCon();
}
System.out.println("Fail to get DB con, too many con: " + nConNum + " max:" + nMaxCon);
return null;
}
/** *//**销毁连接
* @param DBPoolCon pCon*/
synchronized public void release(DBPoolCon pCon) {
pCon.nUsedTimes++;
if (pCon.nUsedTimes > nMaxUsedTime) {//如果使用时间大于最大使用时间,则该连接被回收
try {
nConNum--;
pCon.con.close();
System.out.println("DB Con closed for used out");
} catch (Exception e) {
System.err.println("Fail to close DB Con");
}
} else { //否则,该连接被重新分配
listCon.add(pCon);
}
}
/** *//**建立连接容器
* @return DBPoolCon 返回一个连接容器*/
protected DBPoolCon createCon() {
DBPoolCon pCon = new DBPoolCon();
try {
pCon.con = DriverManager.getConnection(DBUrl, DBUser, DBPasswd);
pCon.con.setAutoCommit(true);
nConNum++;
System.out.println("DB Con created, con num:" + nConNum + " max:" + nMaxCon);
} catch (Exception e) {
System.err.println("Fail to create DB Con");
}
return pCon;
}
/** *//**回收器
* 将连接池中的所有连接关闭*/
protected void finalize() {
try {
while (listCon.size() > 0) {
DBPoolCon pCon = (DBPoolCon) listCon.removeFirst();
pCon.con.close();
}
} catch (Exception e) {
System.err.println("Fail to close DB Con");
}
}
}
package db.khan;
import java.sql.*;
/**//*数据库连接容器
* */
public class DBPoolCon {
/**//*容器中的连接*/
public Connection con = null;
public int nUsedTimes = 0;
DBPoolCon() {
}
public Connection get() {
return con;
}
}
一个操作和管理连接容器的连接池
package db.khan;
import java.sql.*;
import java.util.*;
/** *//**连接池类
* */
public class DBConPool {
protected LinkedList listCon = new LinkedList();// 一个存放链接池的链表
protected String DBUrl = null;
protected String DBUser = null;
protected String DBPasswd = null;
/** *//**最大连接数*/
protected int nMaxCon = 0;
/** *//**连接最大使用时间*/
protected int nMaxUsedTime = 0;
/** *//**当前已用连接数*/
protected int nConNum = 0;
/** *//**构造器
* @param String DBUrl
* @param String DBUser
* @param String DBPasswd
* @param int nMaxCon
* @param int nMaxUsedTime*/
DBConPool(String DBUrl, String DBUser, String DBPasswd, int nMaxCon, int nMaxUsedTime) {
this.DBUrl = DBUrl;
this.DBUser = DBUser;
this.DBPasswd = DBPasswd;
this.nMaxCon = nMaxCon;
this.nMaxUsedTime = nMaxUsedTime;
}
/** *//**从连接池中取得连接(线程安全函数)
* @return DBPoolCon 返回的数据库连接容器
* */
synchronized public DBPoolCon get() {
if (listCon.size() > 0) {//有连接时,直接取得连接
return (DBPoolCon) listCon.removeFirst();
}
if (nConNum < nMaxCon) {//如果没有多余连接,但连接池未满,新建一个连接
return createCon();
}
System.out.println("Fail to get DB con, too many con: " + nConNum + " max:" + nMaxCon);
return null;
}
/** *//**销毁连接
* @param DBPoolCon pCon*/
synchronized public void release(DBPoolCon pCon) {
pCon.nUsedTimes++;
if (pCon.nUsedTimes > nMaxUsedTime) {//如果使用时间大于最大使用时间,则该连接被回收
try {
nConNum--;
pCon.con.close();
System.out.println("DB Con closed for used out");
} catch (Exception e) {
System.err.println("Fail to close DB Con");
}
} else { //否则,该连接被重新分配
listCon.add(pCon);
}
}
/** *//**建立连接容器
* @return DBPoolCon 返回一个连接容器*/
protected DBPoolCon createCon() {
DBPoolCon pCon = new DBPoolCon();
try {
pCon.con = DriverManager.getConnection(DBUrl, DBUser, DBPasswd);
pCon.con.setAutoCommit(true);
nConNum++;
System.out.println("DB Con created, con num:" + nConNum + " max:" + nMaxCon);
} catch (Exception e) {
System.err.println("Fail to create DB Con");
}
return pCon;
}
/** *//**回收器
* 将连接池中的所有连接关闭*/
protected void finalize() {
try {
while (listCon.size() > 0) {
DBPoolCon pCon = (DBPoolCon) listCon.removeFirst();
pCon.con.close();
}
} catch (Exception e) {
System.err.println("Fail to close DB Con");
}
}
}