/// <summary> /// 释放数据库访问连接对象 /// </summary> /// <param name="con">数据库访问连接对象</param> /// <param name="visitType">数据库访问类型</param> internal void ReleaseDbConnection(DbConnection con, DBVisitType visitType) { if (visitType == DBVisitType.R) { if (this._config.ReadConCount < DBConstant.ReadConCount) { con.Close(); } else { this._readConPool.Add(con); } } else if (visitType == DBVisitType.W) { if (this._config.WriteConCount < DBConstant.WriteConCount) { con.Close(); } else { this._writeConPool.Add(con); } } else { throw new NotSupportedException(string.Format("不支持的访问类型:{0}", visitType.ToString())); } }
/// <summary> /// 获取数据库访问连接对象 /// </summary> /// <param name="visitType">数据库访问类型</param> /// <returns>数据库访问连接对象</returns> internal DbConnection GetDbConnection(DBVisitType visitType) { DbConnection con = null; switch (visitType) { case DBVisitType.R: if (this._config.ReadConCount < DBConstant.ReadConCount) { con = this.CreateConnection(this._config, this._readConStr); } else { if (!this._readConPool.TryTake(out con, this._config.GetConTimeout)) { throw new ApplicationException("从连接池获取读连接超时"); } } break; case DBVisitType.W: if (this._config.WriteConCount < DBConstant.WriteConCount) { con = this.CreateConnection(this._config, this._writeConStr); } else { if (!this._writeConPool.TryTake(out con, this._config.GetConTimeout)) { throw new ApplicationException("从连接池获取写连接超时"); } } break; default: throw new NotSupportedException(string.Format("不支持的访问类型:{0}", visitType.ToString())); } try { if (con.State == ConnectionState.Closed) { con.Open(); } } catch (Exception ex) { this.ReleaseDbConnection(con, visitType); throw new ApplicationException("打开数据库连接异常", ex); } return(con); }