Domino-斌少
作者Domino-斌少·2013-09-02 09:54
软件开发工程师·世强先进

JDBC&连接池 数据交互改造之旅- 连接池管理类优化(内置配置文件版)

字数 6006阅读 3142评论 1赞 1

package com.landraydev.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

public class DBConnectionPoolManager {
 static private DBConnectionPoolManager instance;
 static private int clients;
 private Vector dbdrivers = new Vector();
 private static Hashtable pools = new Hashtable();
 public static final String CONFIG_JDBC_PROPERTIES = "/config/jdbc.properties";

 /**
  * 连接池管理类实例化 注释:进行连接池资源的统一管理
  */
 private DBConnectionPoolManager() {
  // TODO Auto-generated constructor stub
  this.InitPools();
 }

 /**
  * 统一管理类实例进行连接池管理 注释:
  */
 static synchronized public DBConnectionPoolManager getInstance() {
  if (instance == null) {
   instance = new DBConnectionPoolManager();
  }
  return instance;
 }

 /**
  * 初始化资源池 注释:根据系统中设置的数据库驱动信息,初始化连接池
  */
 private void InitPools() {
  // TODO Auto-generated method stub
  this.loadDBDrvers();
  Iterator alldbdrivers = dbdrivers.iterator();
  while (alldbdrivers.hasNext()) {
   this.createPool((DBConfig) alldbdrivers.next());
   // System.out.println("创建资源池");
  }
  System.out.println("资源池创初始化完成!");
 }

 /**
  * 创建指定数据库连接池 注释:根据数据库驱动对象来创建连接池资源
  */
 private void createPool(DBConfig dbconfig) {

  // TODO Auto-generated method stub
  DBConnectionPool dbcp = new DBConnectionPool(dbconfig.getPoolname(),
    dbconfig.getDbdriver(), dbconfig.getDburl(),
    dbconfig.getDbuser(), dbconfig.getDbpassword(),
    dbconfig.getMaxconn());
  this.pools.put(dbconfig.getPoolname(), dbcp);
 }

 /**
  * 加载配置信息中的数据库驱动信息 注释:加载外部文件中数据库驱动信息,后续支持外置文件 poolName:资源池名称
  * dbDriver:数据库低层驱动类 dbUrl:数据库连接 userName:数据库用户 userPwd:数据库用户密码
  * connMax:资源池最大连接资源数
  */
 private void loadDBDrvers() {
  // TODO Auto-generated method stub
  DBConfig dbc = null;
  String poolName = "";
  String dbDriver = "";
  String dbURL = "";
  String userName = "";
  String userPwd = "";
  String connMax = "";
  /* 外部properties文件配置连接资源池 */
  Properties prop = new Properties();
  InputStream in = getClass().getResourceAsStream(CONFIG_JDBC_PROPERTIES);
  try {
   prop.load(in);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("不能够读取默认数据库连接池配置文件,请确认文件是否存在:"
     + CONFIG_JDBC_PROPERTIES);
  }

  /* 接口容器中对设置项逐个解析,获取对应的资源池设置信息 */
  Enumeration poolConfigs = (Enumeration) prop.propertyNames();
  while (poolConfigs.hasMoreElements()) {
   String name = (String) poolConfigs.nextElement();

   if (name.endsWith("poolName")) {
    poolName = prop.getProperty(name);
    dbDriver = prop.getProperty(poolName + ".dbDriver");
    dbURL = prop.getProperty(poolName + ".dbUrl");
    userName = prop.getProperty(poolName + ".dbUser");
    userPwd = prop.getProperty(poolName + ".dbPassword");
    connMax = prop.getProperty(poolName + ".maxConn", "0");

    int maxConn = Integer.valueOf(connMax).intValue();

    // System.out.println(poolName + "=" + userName + "=" +
    // dbDriver);
    /* 创建数据库对象 */
    dbc = new DBConfig(poolName, dbDriver, dbURL, userName,
      userPwd, maxConn);

    /* 加入资源池队列中 */
    this.dbdrivers.add(dbc);
   }
  }

 }

 /**
  * 获取指定资源池中的链接资源
  */
 public Connection getPoolConnection(String poolname) {
  DBConnectionPool pool = null;
  Connection con = null;
  pool = (DBConnectionPool) pools.get(poolname);
  if (pool != null) {
   con = pool.getConnection();
   if (con != null) {
    clients++;
    System.out.println("已获取连接池:" + poolname + "连接资源!");
   }
  }
  return con;
 }

 /**
  * 规定时间内获取指定连接池中的连接资源 注释:timeout 单位 为毫秒
  */
 public Connection getPoolConnection(String poolname, long timeout) {
  DBConnectionPool pool = null;
  Connection con = null;
  pool = (DBConnectionPool) pools.get(poolname);
  System.out.println(pool.getPoolname());
  con = pool.getConnection(timeout);
  if (con != null) {
   clients++;
   System.out.println("已获取连接池:" + poolname + "连接资源!");
  }
  return con;
 }

 /**
  * 返还连接资源给指定连接池 注释:连接资源使用完后,将连接资源返还给连接池,给其他业务使用
  */
 public void freePoolConnection(String poolname, Connection conn) {
  DBConnectionPool pool = (DBConnectionPool) pools.get(poolname);
  if (pool != null)
   pool.freeConnection(conn);
  clients--;
 }

 /**
  * 释放指定连接池中的所有连接资源 注释:释放当前连接池中闲置的连接资源
  */
 public static void releaseConnectionPool(String poolname) {
  DBConnectionPool pool = (DBConnectionPool) pools.get(poolname);
  if (pool != null)
   pool.releasePool();
 }

 /**
  * @param clients
  *            the clients to set
  * @return
  */
 public static int getClients() {
  return clients;
 }
}

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论1 条评论

synge6071synge6071软件开发工程师昆明锐祺电脑有限公司
2014-03-07 14:17
DBConnectionPool 这个类在哪里?
Ctrl+Enter 发表

作者其他文章

相关问题

相关资料

X社区推广