<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    codefans

    導(dǎo)航

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    統(tǒng)計(jì)

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    程序設(shè)計(jì)鏈接

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    一個(gè)簡(jiǎn)單的pool代碼

    the detail pool management code

    DBConnectionManager.java

    package com.coa.cim.database;

    /**
    * <p>Title: CIM SYSTEM</p>
    * <p>Description: The Customer Infomation Managment System</p>
    * <p>Copyright: Copyright (c) 2002</p>
    * <p>Company: COA Sci&Tech</p>
    * @author Mula Liu
    * @version 1.0
    */

    import java.sql.*;
    import java.util.*;
    import java.io.*;

    public class DBConnectionManager {


    private static DBConnectionManager instance=null;
    private DBConnectionPool pool;
    private static int client;
    private Properties dbProps;
    private Vector drivers;

    public DBConnectionManager() {
    init();
    }

    public synchronized static DBConnectionManager getInstance(){
    if(instance==null){
    instance=new DBConnectionManager();
    }
    client++;
    return(instance);
    } //create an instance of connection manager. if exits ,just returen the instance

    void init(){
    drivers=new Vector();
    InputStream is=this.getClass().getResourceAsStream("../resource/Res.properties");
    try{
    dbProps=new Properties();
    dbProps.load(is);
    }catch(Exception ex){
    System.out.println("Miss Resource File "+ex.getMessage());
    }
    loadDriver();
    createPool();
    } //using Properties.load() method to locate outter properties file

    public void loadDriver(){
    String driverClasses=dbProps.getProperty("dbDriver");
    StringTokenizer st =new StringTokenizer(driverClasses);
    while(st.hasMoreElements()){
    String driverClassName=st.nextToken().trim();
    try{
    Driver driver=(Driver)Class.forName(driverClassName).newInstance();
    DriverManager.registerDriver(driver);
    drivers.addElement(driver);
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    } //parse the file, load mutil driver class in

    public void createPool(){
    String userName=dbProps.getProperty("dbUserName");
    String password=dbProps.getProperty("dbPassword");
    String url=dbProps.getProperty("connectionURL");
    int maxConn;
    try{
    maxConn=Integer.valueOf(dbProps.getProperty("maxConnection","0")).intValue();
    }catch(NumberFormatException ex){
    maxConn=0;
    }
    pool=new DBConnectionPool(userName,password,url,maxConn);
    } //parse the file, load username,password,url and maxconnection in

    public synchronized int getClientCount(){
    return(client);
    }

    public Connection getDBConnection(){
    if(pool != null){
    return(pool.getDBConnection());
    }
    return(null);
    }//act as facade

    public Connection getDBConnection(long timeout){
    if(pool != null){
    return(pool.getDBConnection(timeout));
    }
    return(null);
    }//act as facade

    public void freeDBConnection(Connection conn){
    if(pool != null){
    pool.freeDBConnection(conn);
    }
    }//act as facade

    public void realse(){
    if(this.client != 0){
    return;
    }
    if(pool != null){
    pool.release();
    Enumeration enum=drivers.elements();
    while(enum.hasMoreElements()){
    Driver driver=(Driver)enum.nextElement();
    try{
    DriverManager.deregisterDriver(driver);
    }catch(Exception ex){
    System.out.println("Can not deregister driver "+driver.getClass().getName());
    }
    }
    }
    }//act as facade then de register driver

    }


    ________________________________________

    DBConnectionPool.java

    package com.coa.cim.database;

    /**
    * <p>Title: CIM SYSTEM</p>
    * <p>Description: The Customer Infomation Managment System</p>
    * <p>Copyright: Copyright (c) 2002</p>
    * <p>Company: COA Sci&Tech</p>
    * @author Mula Liu
    * @version 1.0
    */

    import java.sql.*;
    import java.util.*;

    public class DBConnectionPool {

    private String dbUserName;
    private String dbPassword;
    private String connectionURL;
    private int maxConnection;
    private Vector freeConnections;
    private int checkedOut;

    public DBConnectionPool(String userName,String password,String url,int maxConn) {
    this.dbUserName=userName;
    this.dbPassword=password;
    this.connectionURL=url;
    this.maxConnection=maxConn;
    freeConnections=new Vector();
    }// initalize

    public synchronized Connection getDBConnection(){
    Connection conn=null;
    if(freeConnections.size() > 0){
    conn=(Connection)freeConnections.elementAt(0);
    freeConnections.removeElementAt(0);
    try{
    if(conn.isClosed()){
    conn=getDBConnection();
    }
    }catch(SQLException ex){
    conn=getDBConnection();
    }
    }else if(maxConnection==0 || checkedOut < maxConnection){
    conn=newDBConnection();
    }
    if(conn!=null){
    checkedOut++;
    }
    return(conn);
    }// using FIFO method to get connection instance

    public synchronized Connection getDBConnection(long timeout){
    long startTime=new java.util.Date().getTime();
    Connection conn;
    while((conn=getDBConnection())==null){
    try{
    wait(timeout);
    }catch(InterruptedException ex){}
    if(new java.util.Date().getTime()-startTime >= timeout){
    return(null);
    }
    }
    return conn;
    }

    public Connection newDBConnection(){
    Connection conn=null;
    try{
    if(dbUserName==null){
    conn=DriverManager.getConnection(connectionURL);
    }else{
    conn=DriverManager.getConnection(connectionURL,dbUserName,dbPassword);
    }
    }catch(SQLException ex){
    ex.printStackTrace();
    }
    return(conn);
    }

    public synchronized void freeDBConnection(Connection conn){
    freeConnections.addElement(conn);
    checkedOut--;
    notifyAll();
    }

    public synchronized void release(){
    Enumeration allConnections=freeConnections.elements();
    while(allConnections.hasMoreElements()){
    try{
    Connection conn=(Connection)allConnections.nextElement();
    conn.close();
    }catch(SQLException ex){
    ex.printStackTrace();
    }
    }
    freeConnections.removeAllElements();
    }

    }

    posted on 2005-11-22 11:23 春雷的博客 閱讀(120) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲高清无码专区视频| 久爱免费观看在线网站| 日本无吗免费一二区| 亚洲人成电影在线观看网| 性色午夜视频免费男人的天堂| 中文字幕中韩乱码亚洲大片 | 亚洲一级特黄无码片| 亚洲av成人片在线观看| 在线观看国产情趣免费视频| 亚洲色大成网站www尤物| 成人免费午间影院在线观看| 亚洲一卡2卡3卡4卡5卡6卡| 日韩免费一级毛片| 日本系列1页亚洲系列| 亚洲免费日韩无码系列 | 在线成人爽a毛片免费软件| 亚洲一卡2卡3卡4卡国产网站| 免费鲁丝片一级在线观看| 黄床大片30分钟免费看| 国产亚洲精品线观看动态图| 少妇人妻偷人精品免费视频 | 亚洲国产成人久久一区久久| 中文字幕免费在线观看动作大片 | 啦啦啦中文在线观看电视剧免费版| 中国亚洲呦女专区| 国产99视频免费精品是看6| 免费人成激情视频在线观看冫| 久久精品九九亚洲精品| 免费观看的毛片手机视频| 国产免费牲交视频免费播放| 亚洲综合无码一区二区| 日韩一区二区在线免费观看 | 日韩在线不卡免费视频一区| 免费观看久久精彩视频| 久久国产亚洲精品无码| 国产成人免费网站在线观看 | 亚洲日本乱码卡2卡3卡新区| 免费又黄又爽又猛的毛片| 成全动漫视频在线观看免费高清版下载 | 1000部拍拍拍18勿入免费视频下载 | 日韩亚洲国产综合久久久|