quot;没有可用的数据库连接");
}
System.out.println("get connection");
return conn;
}
/**
* 关闭该连接池中的所有数据库连接
* @throws SQLException
*/
public synchronized void close() throws SQLException
{
this.isflag = false;
SQLException excp = null;
//关闭空闲池
Iterator iter = m_instance.FreeConnectionPool.iterator();
while(iter.hasNext()){
try{
((_Connection)iter.next()).close();
System.out.println("close connection:free");
m_instance.current_conn_count --;
}catch(Exception e){
if(e instanceof SQLException)
excp = (SQLException)e;
}
}
//关闭在使用的连接池
iter = m_instance.ConnectionPool.iterator();
while(iter.hasNext()){
try{
((_Connection)iter.next()).close();
System.out.println("close connection:inused");
m_instance.current_conn_count --;
}catch(Exception e){
if(e instanceof SQLException)
excp = (SQLException)e;
}
}
if(excp != null)
throw excp;
}
/**
* 返回是否支持事务
* @return boolean
*/
public boolean isSupportTransaction() {
return m_instance.supportTransaction;
}
/**
* 连接池调度管理
*
*/
public void schedule()
{
Connection conn = null;
//再检查是否能够分配
Iterator iter = null;
//检查是否有已经归还的连接
{
iter = m_instance.ConnectionPool.iterator();
while(iter.hasNext()){
_Connection _conn = (_Connection)iter.next();
if(!_conn.isFree()){
conn = _conn.getFreeConnection();
_conn.setIsFree(false);
m_instance.ConnectionPool.remove(_conn);
m_instance.FreeConnectionPool.add(_conn);
break;
}
}
}
if (m_instance.current_conn_count < m_instance.MaxConnectionCount)
{
//新建连接到空闲连接池
int newcount = 0 ;
//取得要建立的数目
if (m_instance.MaxConnectionCount - m_instance.current_conn_count
>=m_instance.MinConnectionCount)
{
newcount = m_instance.MinConnectionCount;
}
else
{
newcount = m_instance.MaxConnectionCount - m_instance.current_conn_count;
}
//创建连接
for (int i=0;i <newcount; i++)
{
_Connection _conn = _Connection.getConnection(m_instance, m_instance.connparam);
m_instance.FreeConnectionPool.add(_conn);
m_instance.current_conn_count ++;
}
}
}
}
ConnectionParam.java
package scut.ailab.connectionpool;
import java.io.Serializable;
/**
* @author youyongming
* 实现数据库连接的参数类
*/
public class ConnectionParam implements Serializable {
private String driver; //数据库驱动
程序private String url; //数据连接的URL
private String user; //数据库用户名
private String password; //数据库密码
/**
* 唯一的构造函数,需要指定连接的四个必要参数
* @param driver 数据驱动
* @param&nb