⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionpool.java

📁 Java实现的数据库连接池原代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     */
    public void set_String(String _ConnString, String _DriveString) throws StateException {
        if (_ConnString != null && _DriveString != null &&
            (_PoolState == PoolState_UnInitialize || _PoolState == PoolState_Stop)) {
            this._ConnString = _ConnString;
            this._DriveString = _DriveString;
        } else {
            throw new StateException(); //服务状态错误
        }
    }
    /**
     * 设置每个连接生存期限(单位分钟),默认20分钟
     * @param _Exist int 连接生存期限
     * @throws StateException 服务状态错误
     */
    public void set_Exist(int _Exist) throws StateException {
        if (_PoolState == PoolState_Stop) {
            this._Exist = _Exist;
        } else {
            throw new StateException(); //服务状态错误
        }
    }
    /**
     * 设置最小实际连接数
     * @param _MinConnection int 最小实际连接数
     * @throws ParameterBoundExecption 参数范围应该在 0~MaxConnection之间,并且应该大于KeepConnection
     */
    public void set_MinConnection(int _MinConnection) throws ParameterBoundException {
        if (_MinConnection < _MaxConnection && _MinConnection > 0 && _MinConnection >= _KeepRealConnection)
            this._MinConnection = _MinConnection;
        else
            throw new ParameterBoundException(); //参数范围应该在 0~MaxConnection之间,并且应该大于KeepConnection
    }
    /**
     * 可以被重复使用次数(引用记数)当连接被重复分配该值所表示的次数时,该连接将不能被分配出去。
     * 当连接池的连接被分配尽时,连接池会在已经分配出去的连接中,重复分配连接(引用记数)。来缓解连接池压力
     * @param _MaxRepeatDegree int 引用记数
     * @throws ParameterBoundExecption 重复引用次数应大于0
     */
    public void set_MaxRepeatDegree(int _MaxRepeatDegree) throws ParameterBoundException {
        if (_MaxRepeatDegree > 0)
            this._MaxRepeatDegree = _MaxRepeatDegree;
        else
            throw new ParameterBoundException(); //重复引用次数应大于0
    }
    /**
     * 设置最大可以创建的实际连接数目
     * @param _MaxConnection int 最大可以创建的实际连接数目
     * @throws ParameterBoundExecption 参数范围错误,参数应该大于MinConnection
     */
    public void set_MaxConnection(int _MaxConnection) throws ParameterBoundException {
        if (_MaxConnection > _MinConnection && _MaxConnection > 0)
            this._MaxConnection = _MaxConnection;
        else
            throw new ParameterBoundException(); //参数范围错误,参数应该大于MinConnection
    }
    /**
     * 设置保留的实际空闲连接,以供可能出现的ReadOnly使用
     * @throws ParameterBoundExecption 保留连接数应大于等于0,同时小于MaxConnection
     */
    public void set_KeepRealConnection(int _KeepRealConnection) throws ParameterBoundException {
        if (_KeepRealConnection >= 0 && _KeepRealConnection < _MaxConnection)
            this._KeepRealConnection = _KeepRealConnection;
        else
            throw new ParameterBoundException(); //保留连接数应大于等于0,同时小于MaxConnection
    }
    /**
     * 设置每次创建连接的连接数
     * @param _SeepConnection int 每次创建连接的连接数
     * @throws ParameterBoundExecption 保留连接数应大于等于0,同时小于MaxConnection
     */
    public void set_SeepConnection(int _SeepConnection) throws ParameterBoundException {
        if (_SeepConnection > 0 && _SeepConnection < _MaxConnection)
            this._SeepConnection = _SeepConnection;
        else
            throw new ParameterBoundException(); //保留连接数应大于等于0,同时小于MaxConnection
    }
    /**
     * 设置登陆数据库的密码
     * @param userID String 数据库登陆帐户
     * @param password String 数据库登陆密码
     * @throws PoolNotStopException 服务尚在运行中
     */
    public void set_DataBaseUser(String userID, String password) throws PoolNotStopException {
        if (_PoolState == PoolState_UnInitialize || _PoolState == PoolState_Stop) {
            this._userID = userID;
            this._password = password;
        } else
            throw new PoolNotStopException();
    }
    /**
     * 得到自动清理连接池的时间间隔
     * @return double 自动清理连接池的时间间隔
     */
    public long get_Interval() {
        return threadCheck.getInterval();
    }
    /**
     * 设置自动清理连接池的时间间隔
     * @param value int 保留的实际空闲连接数
     */
    public void set_Interval(long value) {
        threadCheck.setInterval(value);
    }
    /**
     * 得到连接池使用的连接字符串
     * @return String 连接字符串
     */
    public String get_ConnString() {
        return _ConnString;
    }
    /**
     * 得到连接池使用的驱动字符串
     * @return String 连接池使用的驱动字符串
     */
    public String get_DriveString() {
        return _DriveString;
    }
    /**
     * 得到每个连接生存期限(单位分钟),默认20分钟
     * @return int 每个连接生存期限(单位分钟)
     */
    public int get_Exist() {
        return _Exist;
    }
    /**
     * 获得连接池已经分配多少只读连接
     * @return int 已经分配多少只读连接
     */
    public int get_ReadOnlyFormPool() {
        return _ReadOnlyFormPool;
    }
    /**
     * 连接池中存在的实际连接数(包含失效的连接)
     * @return int 实际连接数
     */
    public int get_RealFormPool() {
        return _RealFormPool;
    }
    /**
     * 得到每次创建连接的连接数
     * @return int 每次创建连接的数目
     */
    public int get_SeepConnection() {
        return _SeepConnection;
    }
    /**
     * 得到目前可以提供的连接数
     * @return int 得到目前可以提供的连接数
     */
    public int get_SpareFormPool() {
        return _SpareFormPool;
    }
    /**
     * 获得空闲的实际连接
     * @return int 空闲的实际连接
     */
    public int get_SpareRealFormPool() {
        return _SpareRealFormPool;
    }
    /**
     * 得到服务器运行时间
     * @return Date 返回服务器运行时间
     */
    public Date get_StartTime() {
        return _StartTime;
    }
    /**
     * 获得已经分配的连接数
     * @return int 已经分配的连接数
     */
    public int get_UseFormPool() {
        return _UseFormPool;
    }
    /**
     * 获得已分配的实际连接
     * @return int 已分配的实际连接
     */
    public int get_UseRealFormPool() {
        return _UseRealFormPool;
    }
    /**
     * 得到当前连接池的状态
     * @return int 连接池的状态
     */
    public int get_PoolState() {
        return _PoolState;
    }
    /**
     * 得到连接池中存在的实际连接数(有效的实际连接)
     * @return int 实际连接数
     */
    public int get_PotentRealFormPool() {
        return _PotentRealFormPool;
    }
    /**
     * 得到最小实际连接数
     * @return int 最小实际连接数
     */
    public int get_MinConnection() {
        return _MinConnection;
    }
    /**
     * 得到每个实际连接可以被重复使用次数(引用记数)
     * @return int 每个实际连接可以被重复使用次数(引用记数)
     */
    public int get_MaxRepeatDegree() {
        return _MaxRepeatDegree;
    }
    /**
     * 得到最大实际连接数目
     * @return int 最大可以创建的实际连接数目
     */
    public int get_MaxConnection() {
        return _MaxConnection;
    }
    /**
     * 得到保留的实际空闲连接
     * @return int 保留的实际空闲连接
     */
    public int get_KeepRealConnection() {
        return _KeepRealConnection;
    }
    /**
     * 得到数据库登陆密码
     * @return String 数据库登陆密码
     */
    public String get_password() {
        return _password;
    }
    /**
     * 得到数据库登陆帐号
     * @return String 数据库登陆帐号
     */
    public String get_userID() {
        return _userID;
    }
    /**
     * 得到连接池最多可以提供多少个连接,该值是最大实际连接数与每个连接可以被重复使用次数的乘积
     * @return int 连接池最多可以提供多少个连接
     */
    public int get_MaxConnectionFormPool() {
        return _MaxConnection * _MaxRepeatDegree;
    }
    //-------------------------------------------------------------------------------------------
    public Connection GetConnectionFormPool(Object key) throws StateException, PoolFullException, KeyException, OccasionException, ConnLevelException {
        return GetConnectionFormPool(key, ConnLevel_None);
    }
    /**
     * 在连接池中申请一个连接,线程安全
     * @param key Object 发起者
     * @return Connection 申请到的连接
     * @throws StateException 服务状态错误
     * @throws PoolFullException 连接池已经饱和,不能提供连接
     * @throws KeyExecption 一个key对象只能申请一个连接
     * @throws OccasionExecption 连接资源耗尽,或错误的访问时机。
     * @throws ConnLevelExecption 无效的错误的级别参数
     */
    public Connection GetConnectionFormPool(Object key, int connLevel) throws StateException, PoolFullException, KeyException, OccasionException, ConnLevelException {
        synchronized (this) {
            if (_PoolState != PoolState_Run)
                throw new StateException(); //服务状态错误
            if (hs_UseConn.size() == get_MaxConnectionFormPool())
                throw new PoolFullException(); //连接池已经饱和,不能提供连接
            if (hs_UseConn.containsKey(key))
                throw new KeyException(); //一个key对象只能申请一个连接

            if (connLevel == ConnLevel_ReadOnly)
                return GetConnectionFormPool_ReadOnly(key); //ReadOnly级别
            else if (connLevel == ConnLevel_High)
                return GetConnectionFormPool_High(key); //High级别
            else if (connLevel == ConnLevel_None)
                return GetConnectionFormPool_None(key); //None级别
            else if (connLevel == ConnLevel_Bottom)
                return GetConnectionFormPool_Bottom(key); //Bottom级别
            else
                throw new ConnLevelException(); //无效的错误的级别参数
        }
    }
    /**
     * 申请一个连接资源ReadOnly级别,只读方式,线程安全
     * @param key Object 申请者
     * @return Connection 申请到的连接对象
     * @throws OccasionExecption 连接资源耗尽,或错误的访问时机。
     */
    private Connection GetConnectionFormPool_ReadOnly(Object key) throws OccasionException {
        ConnStruct cs = null;
        for (int i = 0; i < al_All.size(); i++) {
            cs = al_All.get(i);
            if (cs.GetEnable() == false || cs.GetAllot() == false ||
                cs.GetUseDegree() == _MaxRepeatDegree || cs.GetIsUse() == true)
                continue;
            return GetConnectionFormPool_Return(key, cs, ConnLevel_ReadOnly); //返回得到的连接
        }
        return GetConnectionFormPool_Return(key, null, ConnLevel_ReadOnly);
    }
    /**
     * 申请一个连接资源High级别,只读方式,线程安全
     * @param key Object 申请者
     * @return Connection 申请到的连接对象
     * @throws OccasionExecption 连接资源耗尽,或错误的访问时机。
     */
    private Connection GetConnectionFormPool_High(Object key) throws OccasionException {
        ConnStruct cs = null;
        ConnStruct csTemp = null;

        for (int i = 0; i < al_All.size(); i++) {
            csTemp = al_All.get(i);
            if (csTemp.GetEnable() == false || csTemp.GetAllot() == false ||
                csTemp.GetUseDegree() == _MaxRepeatDegree) { //不可以分配跳出本次循环。
                csTemp = null;
                continue;
            }
            if (csTemp.GetUseDegree() == 0) { //得到最合适的
                cs = csTemp;
                break;
            } else { //不是最合适的放置到最佳选择中
                if (cs != null) {
                    if (csTemp.GetUseDegree() < cs.GetUseDegree())
                        //与上一个最佳选择选出一个最佳的放置到cs中
                        cs = csTemp;
                } else
                    cs = csTemp;
            }
        }
        return GetConnectionFormPool_Return(key, cs, ConnLevel_High); //返回最合适的连接
    }
    /**
     * 申请一个连接资源,优先级-None,线程安全
     * @param key Object 申请者
     * @return Connection 申请到的连接对象
     * @throws OccasionExecption 连接资源耗尽,或错误的访问时机。
     */
    private Connection GetConnectionFormPool_None(Object key) throws OccasionException {
        ArrayList<ConnStruct> al = new ArrayList<ConnStruct>();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -