日期:2014-05-16  浏览次数:20457 次

完整的连接池类

Oracle连接池需要odbc连接包

1。读取配置文件

db.properties

drivers=oracle.jdbc.driver.OracleDriver

r2gdb.url=jdbc:oracle:thin:@192.168.1.16:1521:r2gdb
r2gdb.user=boss2
r2gdb.password=boss2
r2gdb.maxConns=100
r2gdb.minConns=2
r2gdb.logInterval=60000


r2gdb_boss.url=jdbc:oracle:thin:@192.168.1.16:1521:r2gdb
r2gdb_boss.user=billing
r2gdb_boss.password=billing
r2gdb_boss.maxConns=100
r2gdb_boss.minConns=2
r2gdb_boss.logInterval=60000

?2.连接池类

package com.database;

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

public class DBConnectionManager
{
  static private DBConnectionManager instance; // 唯一实例
  static private int clients;
  private Vector drivers = new Vector();
  private PrintWriter log;
  private Hashtable pools = new Hashtable();

  /**
   * 返回唯一实例.如果是第一次调用此方法,则创建实例
   *
   * @return DBConnectionManager 唯一实例
   */
  static synchronized public DBConnectionManager getInstance()
  {
    if (instance == null)
    {
        instance = new DBConnectionManager();
    }
    clients++;
    return instance;
  }

  /**
   * 建构函数私有以防止其它对象创建本类实例
   */
  private DBConnectionManager()
  {
    init();
  }

  /**
   * 将连接对象返回给由名字指定的连接池
   *
   * @param name 在属性文件中定义的连接池名字
   * @param con 连接对象
   */
  public void freeConnection(String name, Connection con)
  {
    DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    if (pool != null)
    {
      pool.freeConnection(con);
      //log("连接反池");
    }
  }

  /**
   * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
   * 限制,则创建并返回新连接
   *
   * @param name 在属性文件中定义的连接池名字
   * @return Connection 可用连接或null
   */
  public Connection getConnection(String name)
  {
    DBConnectionPool pool = (DBConnectionPool) 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)
  {
    DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    if (pool != null)
    {
      return pool.getConnection(time);
    }
    return null;
  }

  /**
   * 关闭所有连接,撤销驱动程序的注册
   */
  public synchronized void release()
  {
    // 等待直到最后一个客户程序调用
    if (--clients != 0)
    {
      return;
    }

    Enumeration allPools = pools.elements();
    while (allPools.hasMoreElements())
    {
      DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
      pool.release();
    }
    Enumeration allDrivers = drivers.elements();
    while (allDrivers.hasMoreElements())
    {
      Driver driver = (Driver) allDrivers.nextElement();
      try
      {
        DriverManager.deregisterDriver(driver);
        //log("撤销JDBC驱动程序 " + driver.getClass().getName() + "的注册");
      }
      catch (SQLException e)
      {
        //log(e, "无法撤销下列JDBC驱动程序的注册: " + driver.getClass().getName());
      }
    }
  }

  /**
   * 根据指定属性创建连接池实例.
   *
      * <poolname></poolname> .url         The JDBC URL for the database
      * <poolname></poolname> .user        A database user (optional)
      * <poolname></poolname> .password    A database user password (if user specified)
      * <poolname></poolname> .maxconn     The maximal number of connections (optional)
      *
   * @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"