随笔 - 171  文章 - 257  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(33)

随笔分类(225)

随笔档案(171)

相册

技术

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 441304
  • 排名 - 48

最新随笔

最新评论

阅读排行榜

评论排行榜

一个连接容器,记录连接和连接使用状况
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");
    }

  }

}
posted on 2006-03-05 17:36 Khan 阅读(1727) 评论(0)  编辑 收藏 引用 所属分类: 跨平台开发Java

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理