服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

自己写的一个未成熟的数据库连接池(一)


  源代码:
  
  /*
  * @title 连接池
  * @author: zxg
  * @version 1.0
  * @memo:定义数据库连接及其数据库连接池等
  */
  package com.drsl.db;
  
  import java.io.*;
  import java.sql.*;
  import java.util.*;
  import java.util.date;
  import java.lang.reflect.*;
  import com.mysql.jdbc.driver;
  
  import com.drsl.db.*;
  
  public class connectionmanager {
    
    static private connectionmanager instance; // 唯一实例
    static private int clients;
    static private int maxonlinetime=30*60*1000;
    
    private vector drivers = new vector();
    private hashtable pools = new hashtable();
    private timer checkconntimer=new timer();
    
    static private printwriter log;
    /**
    * 返回唯一实例.如果是第一次调用此方法,则创建实例
    *
    * @return connectionmanager 唯一实例
    */
    static synchronized public connectionmanager getinstance() {
      if (instance == null) {
        instance = new connectionmanager();
      }
  //    clients++;
      return instance;
    }
    
    /**
    * 建构函数私有以防止其它对象创建本类实例
    */
    private connectionmanager() {
      init();
    }
    
    /**
    * 读取属性完成初始化
    */
    private void init() {
      
      try {
        
        inputstream  is = getclass().getresourceasstream("db.properties");
        
        properties  dbprops = new properties();
      
        dbprops.load(is);
      }
      catch (exception e) {
        e.printstacktrace();
        system.err.println("不能读取属性文件= " +
        "请确保db.properties在classpath指定的路径中");
        return;
      }
      string logfile = dbprops.getproperty("logfile", "log.txt");
      try {
        log = new printwriter(new filewriter(logfile, true), true);
      }
      catch (ioexception e) {
        system.err.println("无法打开日志文件: " + logfile);
        log = new printwriter(system.err);
      }
      loaddrivers(dbprops);
      createpools(dbprops);
    }
    
    /**
    * 装载和注册所有jdbc驱动程序
    *
    * @param props 属性
    */
    private void loaddrivers(properties props) {
      string driverclasses = props.getproperty("drivers");
      stringtokenizer st = new stringtokenizer(driverclasses);
      while (st.hasmoreelements()) {
        string driverclassname = st.nexttoken().trim();
        try {
          
          driver driver = (driver)class.forname(driverclassname).newinstance();
          if(driver!=null){
            drivermanager.registerdriver(driver);
            drivers.addelement(driver);
            log("begin");
            log("成功注册jdbc驱动程序" + driverclassname);
          }
          else{
            log("begin");
            log("注册jdbc驱动程序" + driverclassname+"失败");
          }
            
        }
        catch (exception e) {
          log("begin");
          log("无法注册jdbc驱动程序: " + driverclassname + ", 错误: " + e);
        }
      }
    }
    
    /**
    * 根据指定属性创建连接池实例.
    *
    * @param props 连接池属性
    */
    private void createpools(properties props) {
      
      enumeration propnames = props.propertynames();
      while (propnames.hasmoreelements()) {
        string name = (string) propnames.nextelement();
        if (name.endswith(".url")) {
          string poolname = name.substring(0, name.lastindexof("."));
          string url = props.getproperty(poolname + ".url");
          if (url == null) {
            log("没有为连接池" + poolname + "指定url");
            continue;
          }
          
          string user = props.getproperty(poolname + ".user");
          string password = props.getproperty(poolname + ".password");
          
          string maxconn = props.getproperty(poolname + ".maxconn", "0");
          string minconn = props.getproperty(poolname + ".minconn", "10");
          string option=props.getproperty(poolname+".option","");
          int max,min;
          try {
            max = integer.valueof(maxconn).intvalue();
            
          }
          catch (numberformatexception e) {
            log("错误的最大连接数限制: " + maxconn + " .连接池: " + poolname);
            max = 0;
          }
          
          try {
            min = integer.valueof(minconn).intvalue();
            
          }
          catch (numberformatexception e) {
            log("错误的最小连接数限制: " + minconn + " .连接池: " + poolname);
            min = 0;
          }
          
          try{
            connectionpool pool = new connectionpool(poolname, url,user,password,min,max,option);
            
            pools.put(poolname, pool);
            
            //2秒钟后开始每个一分钟检查一次连接池情况
            checkconntimer.schedule(pool,2000,60*1000);
  
            log("成功创建连接池" + poolname);
            
          }catch(exception e){
            log(e,"创建dbconnectionpool出错");
          }
        }
      }
    }
  
    /**
    * 将连接对象返回给由名字指定的连接池
    *
    * @param name 在属性文件中定义的连接池名字
    * @param con 连接对象
    */
    public void freeconnection(string name, connection conn) {
      connectionpool pool = (connectionpool) pools.get(name);
      if (pool != null) {
        pool.freeconnection(conn);
      }
    }
    
    /**
    * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
    * 限制,则创建并返回新连接
    *
    * @param name 在属性文件中定义的连接池名字
    * @return connection 可用连接或null
    */
    public connection getconnection(string name) {
      connectionpool pool = (connectionpool) pools.get(name);
      if (pool != null) {
        return pool.getconnection();
      }
      return null;
    }
    
    /**
    * 获得一个可用连接.若没有可用连接,且已有连接数小于最大连接数限制,
    * 则创建并返回新连接.否则,在指定的时间内等待其它线程释放连接.
    *
    * @param name 连接池名字
    * @param time 以毫秒计的等待时间
    * @return connection 可用连接或null
    */
    public connection getconnection(string name, long time) {
      connectionpool pool = (con

扫描关注微信公众号