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

📄 dbconpool.java

📁 java支持的短信平台
💻 JAVA
字号:
package com.khan.db;

import java.sql.*;
import java.util.*;
import com.khan.file.Log;
import com.khan.util.*;

/**
 *
 * <p>Title:连接池类 </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class DBConPool {

  public static final String MYSQL_DRIVER ="com.mysql.jdbc.Driver";
  public static final String POSTGRES_DRIVER ="org.postgresql.Driver";
  public static final String MSSQL_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

  private Log log=new Log("DBConPollLog.out");
  private Log err=new Log("DBConPollLog.err");
  protected LinkedList listCon = new LinkedList();// 一个存放链接池的链表
  protected String DBUrl = null;
  protected String DBUser = null;
  protected String DBPasswd = null;
  protected String DbDriver =null;

  /**最大连接数*/
  protected int nMaxCon = 0;

  /**连接最大使用时间*/
  protected int nMaxUsedTime = 0;

  /**当前已用连接数*/
  protected int nConNum = 0;

  /**构造器
   * @param DbDriver String
   * @param DBUrl String
   * @param DBUser String
   * @param DBPasswd String
   * @param nMaxCon int
   * @param nMaxUsedTime int*/
  public DBConPool(String DbDriver, 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;
      this.DbDriver = DbDriver;
      try {
          Class.forName(DbDriver);
      } catch (ClassNotFoundException e) {
          err.logOut("装入数据库驱动出错!", e);
      }
  }



  /**从连接池中取得连接(线程安全函数)
   * @return DBPoolCon 返回的数据库连接容器
   * */
  synchronized public DBPoolCon get() {
    DBPoolCon dbp = null;
    //common.Assert("debug:"+Thread.currentThread().getName()+ " 取得连接!");

    for (int i = 0; i < listCon.size(); i++) {//有连接时,直接取得连接
      dbp =  (DBPoolCon) listCon.removeFirst();
      if (login(dbp))  //检查连接的有效性
        return dbp;
    }

    if (nConNum < nMaxCon) {//如果没有多余连接,但连接池未满,新建一个连接
      return createCon();
    }

    err.logOut("获取连接池链接出错, too many con: " + nConNum + " max:" + nMaxCon);
    return null;
  }

  /**销毁连接
   * @param pCon  DBPoolCon*/
  synchronized public void release(DBPoolCon pCon) {
    //pCon.nUsedTimes++;
    //if (pCon.nUsedTimes > nMaxUsedTime) {//如果使用时间大于最大使用时间,则该连接被回收
    //  try {
    //    nConNum--;
    //    pCon.con.close();
    //    log.logOut("连接池链接超过存活期,被回收");
    //  } catch (Exception e) {
    //    err.logOut("回收连接失败",e);
    //  }
    //} else {  //否则,该连接被重新分配
    //common.Assert("debug:"+Thread.currentThread().getName()+"回收db连接");
    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++;
      log.logOut("建立一个连接, con num:" + nConNum + " max:" + nMaxCon);
    } catch (Exception e) {
          err.logOut("建立连接失败",e);
    }
    return pCon;
  }



  protected boolean login(DBPoolCon con) {
    boolean result = true;
    String sqlstr= "select 1";
    Statement stmt = null;
    ResultSet rs = null;

    try {
        stmt = con.get().createStatement();
        rs = stmt.executeQuery(sqlstr);
        if(rs.next()) return true;
    } catch (SQLException e) {
        return false;
    } catch (Exception e){
        return false;
    }

    return result;
  }


  /**回收器
   * 将连接池中的所有连接关闭*/
  protected void finalize() {
    try {
      while (listCon.size() > 0) {
        DBPoolCon pCon = (DBPoolCon) listCon.removeFirst();
        pCon.con.close();
      }
    } catch (Exception e) {
      err.logOut("回收连接池,关闭连接失败", e);
    }
  }
}

⌨️ 快捷键说明

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