选择自 small_ding 的 Blog
自定义Connection Pool的源代码及用法
1.存储基本的数据库连接Bean
package BSC.pool;
import java.sql. *;
import java.io.Serializable;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class ConnBean implements java.io.Serializable {
private Connection conn=null;
private boolean inuse=false;
public ConnBean() {
}
public ConnBean(Connection con)
{
if(con!=null)conn=con;
}
public void setConnection(Connection con)
{
conn=con;
}
public Connection getConnection()
{
return conn;
}
public void setInuse(boolean inuse)
{
this.inuse =inuse;
}
public boolean getInuse()
{
return inuse;
}
public void close()
{
try
{
conn.close();
}
catch(SQLException sqle)
{
System.err.println(sqle.getMessage());
}
}
}
2. 连接池管理Bean
package BSC.pool;
import java.sql. *;
import java.util.*;
import java.lang.InterruptedException;
import java.io.Serializable;
import BSC.pool.ConnBean;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class PoolBean implements java.io.Serializable {
private String driver=null;
private String url=null;
private int size=0;
private String username="";
private String password="";
private ConnBean connBean=null;
private Vector pool=null;
private String dbType="1";
public PoolBean() {
}
public void setDriver(String d)
{
if(d!=null)driver=d;
}
public String getDriver()
{
return driver;
}
public void setURL(String u)
{
if(u!=null)url=u;
}
public String getURL()
{
return url;
}
public void setSize(int s)
{
if(s>1)size=s;
}
public int getSize()
{
return size;
}
public void setUsername(String un)
{
if(un!=null)username=un;
}
public String getUsername()
{
return username;
}
public void setPassword(String pwd)
{
if(pwd!=null)password=pwd;
}
public String getPassword()
{
return password;
}
public void setConnBean(ConnBean cb)
{
if(cb!=null)connBean=cb;
}
public ConnBean getConnBean() throws Exception
{
Connection con=getConnection();
ConnBean cb=new ConnBean(con);
cb.setInuse(true) ;
return cb;
}
private Connection createConnection() throws Exception
{
Connection con=null;
con=DriverManager.getConnection(url,username,password) ;
return con;
}
public synchronized void initializePool() throws Exception
{
if(driver==null)
throw new Exception("没有提供驱动程序名称!");
if(url==null)
throw new Exception("没有提供URL!");
if(size<1)
throw new Exception("连接池大小不能小于一!");
try
{
// DriverManager.registerDriver(new COM.ibm.db2.jdbc.app.DB2Driver());
Class.forName(driver).newInstance();
for(int i=0;i<size;i++)
{
Connection con=createConnection();
if(con!=null)
{
ConnBean cb=new ConnBean(con);
addConnection(cb);
}
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage() );
}
}
private void addConnection(ConnBean cb)
{
if(pool==null)pool=new Vector(size);
pool.addElement(cb);
}
public synchronized void releaseConnection(Connection con)
{
for(int i=0;i<pool.size() ;i++)
{
ConnBean cb=(ConnBean)pool.elementAt(i);
if(cb.getConnection() ==con)
{
System.err.println("释放第"+i+"个连接");
cb.setInuse(false);
break;
}
}
}
public synchronized Connection getConnection()
throws Exception
{
ConnBean cb=null;
for(int i=0;i<pool.size() ;i++)
{
cb=(ConnBean)pool.elementAt(i);
if(cb.getInuse()==false)
{
cb.setInuse(true) ;
Connection con=cb.getConnection();
if(!con.isClosed()){
return con;
}else{
pool.removeElement(cb);
}
}
}
try
{
Connection con=createConnection();
cb=new ConnBean(con);
cb.setInuse(true);
pool.addElement(cb);
}
catch(Exception e)
{
System.err.println(e.getMessage() );
throw new Exception(e.getMessage() );
}
return cb.getConnection() ;
}
public synchronized void emptyPool()
{
for(int i=0;i<pool.size() ;i++)
{
System.err.println("关闭第"+i+"JDBC连接");
ConnBean cb=(ConnBean)pool.elementAt(i);
if(cb.getInuse()==false)
cb.close();
else
{
try
{
java.lang.Thread.sleep(20000);
cb.close();
}
catch(InterruptedException ie)
{
System.err.println(ie.getMessage());
}
}
}
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
}
3.用法
import java.sql.*;
import BSC.pool.*;
…其他代码
Connection conn=null;
try{
poolbean = new PoolBean();
poolbean.setDriver(this.getDriver());
poolbean.setURL(this.getUrl());
poolbean.setSize((new Integer(this.getSize())).intValue());
poolbean.setUsername(this.getUsername());
poolbean.setPassword(this.getPassword());
poolbean.initializePool();
conn=pool.getConnection();
…其他代码
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}