⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionpool.java

📁 采用JAVA开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public int getInitPoolSize() {
    return (initPoolSize);
  }

  /**得到连接池的最大容量。
   * @return 得到连接池的最大容量。
   * */
  public int getMaxPoolSize() {
    return (maxPoolSize);
  }

  /**得到连接池当前的实际容量,或者说连接池中连接的数量。
   * @return 得到连接池当前的实际容量,或者说连接池中连接的数量。
   * */
  public int getPoolSize() {
    return (_poolSize);
  }

  /**得到连接池中激活的连接数。
   * @return 得到连接池中激活的连接数
   * */
  public int getActiveConnCount () {
    return (_activeConnCount);
  }

  /**得到连接池中空闲的连接数。
   * @return 得到连接池中空闲的连接数。
   * */
  public int getFreeConnCount() {
    return (_poolSize-_activeConnCount);
  }





/**
 * 关闭连接池,所有初始化过的连接池都应该在程序结束前关闭
 */
  public synchronized void close () {
    // Stop the background monitoring thread

    if (_runner != null) {
      _runner.interrupt();
    }

    //// log the active connection num
    //log.debug ("Connection Pool closed.");
    cat.debug("Connection Pool closed.");
    //// there should be no connection active, if some connection still active, log the error
    if (_activeConnCount!=0)
      cat.warn("when close the Connection Pool, the active Connection number is "+_activeConnCount);
      //log.warn ("when close the Connection Pool, the active Connection number is "+_activeConnCount);
    if (_poolSize!=initPoolSize)
      cat.warn("Close connectionPool (poolsize="+_poolSize+", initPoolSize="+initPoolSize+" ,maxPoolSize="+maxPoolSize+")");
      //log.warn("Close connectionPool (poolsize="+_poolSize+", initPoolSize="+initPoolSize+" ,maxPoolSize="+maxPoolSize+")");
    ConnectionWrap cw;
    while (!_inactiveConnList.isEmpty()) {
      cw=(ConnectionWrap)_inactiveConnList.removeFirst();
      try {
        cw.conn.close();
      }catch (SQLException e) {/*do nothing*/}
    }//while
    while (!_activeConnList.isEmpty()) {
      cw=(ConnectionWrap)_activeConnList.removeFirst();
      try {
        cw.conn.close();
      }catch (SQLException e) {/*do nothing*/}
    }//while
    _poolSize=0;
    _activeConnCount=0;
    _activeConnList=null;
    _inactiveConnList=null;
  }

/**
 *从连接池得到一个新连接,如果连接池中没有空闲的连接,那么就看连接池是否已达到最大容量了,
 *如果是则返回一个null,否则新建一个连接加入到连接池中。
 *@param owner 是使用这个连接的对象,在debug时候有用。
 *@return 得到一个数据库连接。
 */
  public synchronized Connection getConnection(Object owner) {
    //从空闲链表中找到一个空闲的数据库连接
    //尝试次数为4
    int tryNum = 5;
    ConnectionWrap conn = null;
    for ( int i = 0; i < tryNum; i++ ){
      //非活动连接队列为空
      if (_inactiveConnList.isEmpty()) {
        //未超过最大连接
        if (_poolSize<maxPoolSize) {
          try{
            conn=createConnection();
            ++_poolSize;
            break;
          }catch (SQLException e) {
            cat.error("create connection error", e);
            continue;
          }//try
        }else{
          cat.warn("reach the max connection!");
          try {
            Thread.sleep(200);
          }catch (Exception ex){
            cat.error("", ex);
          }
          continue;
        }
        //if
      }else{
        conn=(ConnectionWrap)_inactiveConnList.removeFirst();
        break;
      }//if
    }
    //如果找到了连接,就把它加入激活链表
    if (conn!=null){
      ++_activeConnCount;   //增加激活连接数目
      _activeConnList.addLast(conn);
      if ( owner instanceof java.lang.Class ){
        Class ownerClass = (Class)owner;
        conn.setOwnerClassName(ownerClass.getName());
      }else{
        conn.setOwnerClassName(owner.getClass().getName());
      }
      conn.setDate();
    }else{
      cat.fatal("can't get a connection!");
    }//if
    return conn;
  }
  public Connection getRealConnection() throws SQLException{
    return dbInfo.getConnection();
  }

  /**
   * 向连接池释放一个连接,一般不会使用到这个函数,这个函数将在申请的连接调用close()方法时自动调用
   * @param conn 要被释放的连接。
   */
  synchronized void releaseConnection(ConnectionWrap conn) {
    //如果能从激活链表中找到这个连接,就把它加到空闲链表中
    if (_activeConnList.remove(conn)) {
      --_activeConnCount;
      conn.setDate();
      _inactiveConnList.addLast(conn);
    }
    else{
    ////log the error for debug
      cat.error("Cannot find the Connection when ConnectionPool release a connection");
      //log.error("Cannot find the Connection when ConnectionPool release a connection");
    }
  }

  public void finalize(){
    this.close();
  }

  public static void main(String args[])
  {
//    ConnectionPool.initial(args[0]);
    try
    {
      Thread.sleep(50000);
    }catch(Exception e){e.printStackTrace();}

    //ConnectionPool.getPool().close();

  }
  public void setInitPoolSize(int initPoolSize) {
    this.initPoolSize = initPoolSize;
  }
  public void setMaxPoolSize(int maxPoolSize) {
    this.maxPoolSize = maxPoolSize;
  }
  public long getOverdurPeriod() {
    return overdurPeriod;
  }
  public void setOverdurPeriod(long overdurPeriod) {
    this.overdurPeriod = overdurPeriod;
  }
  public long getScanPeriod() {
    return scanPeriod;
  }
  public void setScanPeriod(long scanPeriod) {
    this.scanPeriod = scanPeriod;
  }
  public DBInfo getDbInfo() {
    return dbInfo;
  }
  public void setDbInfo(DBInfo dbInfo) {
    this.dbInfo = dbInfo;
  }
  public String getTestCommand() {
    return testCommand;
  }
  public void setTestCommand(String testCommand) {
    this.testCommand = testCommand;
  }
  public void setTestPeriod(long testPeriod) {
    this.testPeriod = testPeriod;
  }
  public long getTestPeriod() {
    return testPeriod;
  }
  public String toString()
  {
    return super.toString() +
    "[scanPeriod=" + scanPeriod + ", overdurPeriod=" + overdurPeriod+
        ",maxPoolSize=" + maxPoolSize + ",initPoolSize=" + initPoolSize +
        ",dbInfo=" + dbInfo +
        ",testPeriod=" + testPeriod + ",testCommand=" + testCommand +
        "]";
  }
  public void init()
      throws SQLException, ClassNotFoundException
  {
    dbInfo.init();
    _activeConnList = new LinkedList();
    _inactiveConnList = new LinkedList();
    _activeConnCount=0;
    _runner=null;

    ConnectionWrap cw;
    for (int i=0;i<_poolSize;++i) {
      _inactiveConnList.addLast(createConnection());
    }
    _runner = new ScanTask();
    _runner.setName("Connection Pool daemon");
    _runner.setDaemon(true);
    _runner.start();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -