tion _conn = new _Connection(param);
return _conn;
}
else
return null;
}
public Connection getFreeConnection() {
//返回数据库连接conn的接管类,以便截住close方法
Connection conn2 = (Connection)Proxy.newProxyInstance(
conn.getClass().getClassLoader(),
conn.getClass().getInterfaces(),this);
return conn2;
}
/**
* 该方法真正的关闭了数据库的连接
* @throws SQLException
*/
void close() throws SQLException{
//由于类属性conn是没有被接管的连接,因此一旦调用close方法后就直接关闭连接
conn.close();
}
public void setIsFree(boolean value)
{
isFree = value;
}
public boolean isFree() {
return isFree;
}
/**
* 判断是否支持事务
* @return boolean
*/
public boolean isSupportTransaction() {
return supportTransaction;
}
}
ConnectionFactory.java
package scut.ailab.connectionpool;
/**
* @author youyongming
*
*/
import java.util.LinkedHashSet;
import java.sql.*;
import java.util.Iterator;
public class ConnectionFactory {
private static ConnectionFactory m_instance = null;
//在使用的连接池
private LinkedHashSet ConnectionPool = null;
//空闲连接池
private LinkedHashSet FreeConnectionPool = null;
//最大连接数
private int MaxConnectionCount = 4;
//最小连接数
private int MinConnectionCount = 2;
//当前连接数
private int current_conn_count = 0;
//连接参数
private ConnectionParam connparam = null;
//是否创建工厂的标志
private boolean isflag = false;
//是否支持事务
private boolean supportTransaction = false;
//定义管理策略
private int ManageType = 0;
private ConnectionFactory() {
ConnectionPool = new LinkedHashSet();
FreeConnectionPool = new LinkedHashSet();
}
/**
* 使用指定的参数创建一个连接池
*/
public ConnectionFactory(ConnectionParam param, FactoryParam fparam)
throws SQLException
{
//不允许参数为空
if ((param == null)||(fparam == null))
throw new SQLException("ConnectionParam和FactoryParam不能为空");
if (m_instance == null)
{
synchronized(ConnectionFactory.class){
if (m_instance == null)
{
//new instance
//参数定制
m_instance = new ConnectionFactory();
m_instance.connparam = param;
m_instance.MaxConnectionCount = fparam.getMaxConn();
m_instance.MinConnectionCount = fparam.getMinConn();
m_instance.ManageType = fparam.getType();
m_instance.isflag = true;
//初始化,创建MinConnectionCount个连接
System.out.println("connection factory 创建!");
try{
for (int i=0; i < m_instance.MinConnectionCount; i++)
{
_Connection _conn = _Connection.getConnectio