DBConnectionPool.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:6k
源码类别:
OA系统
开发平台:
Java
- package com.gforce.currency.database;
- /**
- * <p>Title: GForce Office Automation </p>
- * <p>Description: </p>
- * <p>Copyright: 版权所有 2003 (c) 西安吉力科技发展有限公司 Copyright (c) 2003 GForce Sceince & Technology Co.,LTD</p>
- * <p>Company: 西安吉力科技发展有限公司 (2003 GForce Sceince & Technology Co.,LTD)</p>
- * @author JBuilder Group
- * @version 2.0
- */
- import java.util.*;
- import java.sql.*;
- import com.gforce.currency.*;
- import java.util.Date;
- import java.io.*;
- public class DBConnectionPool
- {
- public Vector FreeConn = new Vector();
- public Vector BusyConn = new Vector();
- public int ConnCount = 0;
- public int intMaxConn;
- public String Name;
- private String Driver;
- private String URL;
- private String User;
- private String Password;
- /**
- * DBConnectionPool构造函数
- * @param Name 连接池名称
- * @param Driver 驱动程序类名称
- * @param URL 连接JDBC URL
- * @param User 用户名
- * @param Password 密码
- * @param intMaxConn 最大连接数
- */
- public DBConnectionPool(String Name, String Driver, String URL, String User,
- String Password, int intMaxConn)
- {
- this.Name = Name;
- this.Driver = Driver;
- this.URL = URL;
- this.User = User;
- this.Password = Password;
- this.intMaxConn = intMaxConn;
- }
- /**
- * 将数据库连接标志为空闲
- * @param ReleaseConn 数据库连接
- */
- public void ReleaseConn(Connection ReleaseConn)
- {
- FreeConn.addElement(ReleaseConn);
- for(int i=0;i<BusyConn.size();i++)
- {
- if(((Vector)BusyConn.get(i)).get(0).equals(ReleaseConn))
- {
- BusyConn.remove(i);
- }
- }
- }
- /**
- * 关闭指定的数据库连接
- * @param Conn 数据库连接
- */
- public void CloseConn(Connection Conn)
- {
- try
- {
- ConnCount--;
- for (int i = 0; i < BusyConn.size(); i++) {
- if ( ( (Vector) BusyConn.get(i)).get(0).equals(Conn)) {
- BusyConn.remove(i);
- }
- }
- Conn.close();
- Conn=null;
- }
- catch(Exception err)
- {
- SystemOut.ErrOut("单个释放连接池名称为:“" + Name + "”的连接时出错!");
- }
- }
- /**
- * 关闭所有的数据库连接
- */
- public void CloseAllConn()
- {
- try
- {
- Connection con = null;
- for (int i = 0; i < FreeConn.size(); i++) {
- try {
- con = (Connection) FreeConn.get(i);
- con.close();
- ConnCount--;
- con = null;
- FreeConn.remove(i);
- }
- catch (Exception err) {
- SystemOut.ErrOut("批量释放连接池名称为:“" + Name + "”的空闲连接时出错!");
- }
- }
- for (int i = 0; i < BusyConn.size(); i++) {
- try {
- con = (Connection) ( (Vector) BusyConn.get(i)).get(0);
- con.close();
- ConnCount--;
- con = null;
- BusyConn.remove(i);
- }
- catch (Exception err) {
- SystemOut.ErrOut("批量释放连接池名称为:“" + Name + "”的繁忙连接时出错!");
- }
- }
- if(ConnCount>0 || BusyConn.size()>0 || FreeConn.size()>0)
- {
- CloseAllConn();
- }
- }
- catch(Exception err)
- {
- SystemOut.ErrOut("关闭连接池名称为:“" + Name + "”的所有连接时出错!");
- }
- }
- /**
- * 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于最大连接
- * 数限制,则创建新连接.如原来登记为可用的连接不再有效
- */
- public synchronized Connection getConnection()
- {
- Connection con = null;
- try
- {
- for (int i = 0; i < BusyConn.size(); i++) {
- int ibe = (new Date()).compareTo(((Vector) BusyConn.get(i)).get(1));
- if (ibe>1000) {
- con = (Connection)((Vector)BusyConn.get(i)).get(0);
- con.close();
- con = null;
- BusyConn.remove(i);
- ConnCount--;
- }
- }
- for (int i = 0; i < FreeConn.size(); i++)
- {
- con = (Connection)FreeConn.get(i);
- if ( con.isClosed())
- {
- con = null;
- FreeConn.remove(i);
- ConnCount--;
- return getConnection();
- }
- }
- if (FreeConn.size() > 0)
- {
- con = (Connection) FreeConn.get(0);
- FreeConn.remove(0);
- Vector vt = new Vector();
- vt.add(con);
- vt.add(new Date());
- BusyConn.add(vt);
- return con;
- }
- if (ConnCount < intMaxConn || intMaxConn == 0)
- {
- con = ConnectionSingle.getConnectionSingle(Driver, URL, User, Password);
- ConnCount++;
- Vector vt = new Vector();
- vt.add(con);
- vt.add(new Date());
- BusyConn.add(vt);
- return con;
- }
- else
- {
- con = getConnection(10);
- return con;
- }
- }
- catch (Exception err)
- {
- SystemOut.ErrOut("创建连接池名称为“" + Name + "”的连接时出错!");
- }
- return con;
- }
- /**
- * 从连接池获取可用连接.可以指定客户程序能够等待的最长时间
- * 参见前一个getConnection()方法.
- * @param timeout 以毫秒计的等待时间限制
- */
- public synchronized Connection getConnection(long timeout)
- {
- long startTime = new Date().getTime();
- Connection con;
- while ( (con = getConnection()) == null)
- {
- if ( (new Date().getTime() - startTime) >= timeout)
- {
- return null;
- }
- }
- return con;
- }
- /**
- * 关闭所有连接
- */
- public synchronized void release()
- {
- Connection con = null;
- for (int i = 0; i < FreeConn.size(); i++)
- {
- try
- {
- con = (Connection) FreeConn.get(i);
- con.close();
- ConnCount--;
- con = null;
- FreeConn.remove(i);
- }
- catch (Exception err)
- {
- SystemOut.ErrOut("批量释放连接池名称为:“" + Name + "”的连接时出错!");
- }
- }
- for (int i = 0; i < BusyConn.size(); i++)
- {
- try
- {
- con = (Connection)((Vector)BusyConn.get(i)).get(0);
- con.close();
- ConnCount--;
- con = null;
- BusyConn.remove(i);
- }
- catch (Exception err)
- {
- SystemOut.ErrOut("批量释放连接池名称为:“" + Name + "”的连接时出错!");
- }
- }
- }
- }