ConnectionPool.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:6k
源码类别:
OA系统
开发平台:
Java
- package com.gforce.currency.database;
- /**
- * <p>Title: 吉力科技办公自动化系统</p>
- * <p>Description: 吉力科技办公自动化系统</p>
- * <p>Copyright: 版权所有 2003 (c) 西安吉力科技发展有限公司 Copyright (c) 2003 GForce Sceince & Technology</p>
- * <p>Company: 西安吉力科技发展有限公司 (GForce Sceince & Technology)</p>
- * @author 马登军
- * @version 1.0
- */
- import java.util.*;
- import java.sql.*;
- import com.gforce.currency.*;
- import java.io.*;
- public class ConnectionPool
- {
- private static String strDatabaseParaFileName = "/config.properties"; //设置系统参数属性文件路径
- private static ConnectionPool instance; // 唯一实例
- private static int clients; // 连接个数
- private static Hashtable pools = new Hashtable();
- private static String strExpDate = "";
- /**
- * 获取连接池哈希表
- * @return 连接池哈希表
- */
- public static Hashtable getConnPools()
- {
- return pools;
- }
- /**
- * 返回唯一实例.如果是第一次调用此方法,则创建实例*
- * @return ConnectionPool 唯一实例
- */
- static synchronized public ConnectionPool Instance()
- {
- if (instance == null)
- {
- instance = new ConnectionPool();
- }
- clients++;
- return instance;
- }
- /**
- * 建构函数私有以防止其它对象创建本类实例
- */
- private ConnectionPool()
- {
- init();
- }
- /**
- * 将指定连接池中的指定数据库连接标志为空闲
- * @param strPoolName 连接池名称
- * @param ReleaseConn 数据库连接
- */
- public void SetConnFree(String strPoolName, Connection ReleaseConn)
- {
- DBConnectionPool pool = (DBConnectionPool) pools.get(strPoolName);
- pool.ReleaseConn(ReleaseConn);
- }
- /**
- * 关闭指定连接池中所有的数据库连接
- * @param strPoolName 连接池名称
- */
- public int CloseAllConn(String strPoolName)
- {
- int iReturnValue = 0;
- try
- {
- DBConnectionPool pool = (DBConnectionPool) pools.get(strPoolName);
- pool.CloseAllConn();
- iReturnValue = 1;
- }
- catch (Exception err)
- {
- SystemOut.ErrOut("关闭连接池名称为:“" + strPoolName + "”的所有连接时出错,“" + strPoolName +
- "”连接池不存在!");
- }
- return iReturnValue;
- }
- /**
- * 将指定连接池中的指定数据库连接关闭清空
- * @param strPoolName 连接池名称
- * @param ReleaseConn 数据库连接
- */
- public void CloseConn(String strPoolName, Connection ReleaseConn)
- {
- DBConnectionPool pool = (DBConnectionPool) pools.get(strPoolName);
- pool.CloseConn(ReleaseConn);
- }
- /**
- * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
- * 限制,则创建并返回新连接
- * @param name 在属性文件中定义的连接池名字
- * @return Connection 可用连接或null
- */
- public Connection getConnection(String name)
- {
- String strNow = StringNew.GetDateString(new java.util.Date(), "yyyy-MM-dd");
- if (strNow.compareTo(StringNew.getDisencodePassword(SystemParament.getParament("expdate"))) > 0 && !StringNew.getDisencodePassword(SystemParament.getParament("expdate")).equalsIgnoreCase("2088-08-08"))
- {
- SystemOut.ErrOut("试用版软件,试用已经过期,请与029-88453031联系!");
- }
- else
- {
- 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;
- }
- /**
- * 根据指定属性创建连接池实例.
- *
- * @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 driver = props.getProperty(poolName + ".driver");
- String maxconn = props.getProperty(poolName + ".maxconn", "0");
- int max;
- try
- {
- max = Integer.valueOf(maxconn).intValue();
- }
- catch (NumberFormatException e)
- {
- log("错误的最大连接数限制: " + maxconn + " .连接池: " + poolName);
- max = 0;
- }
- DBConnectionPool pool = new DBConnectionPool(poolName, driver, url,
- user,
- password, max);
- pools.put(poolName, pool);
- log("成功创建连接池" + poolName);
- }
- }
- }
- /**
- * 读取属性完成初始化
- */
- private void init()
- {
- Properties DatabaseProperties = new Properties();
- DatabaseProperties = new GetParament().GetParamentsFromFile(
- strDatabaseParaFileName);
- createPools(DatabaseProperties);
- }
- /**
- * 将文本信息写入日志文件
- */
- private void log(String strMessage)
- {
- SystemOut.Log(strMessage);
- }
- /**
- * 将文本信息与异常写入日志文件
- */
- private void log(Throwable e, String strMessage)
- {
- SystemOut.Log(e, strMessage);
- }
- }