数据库连接池Data Connection Framework
数据库连接池Data Connection Framework具备Connection pooling机制,能管理不同的ADO.net Connection。并通过池对象工厂去PoolObjectFactory去创建数据连接池。(可参考:对象工厂创建--- Singleton Object,SingleCall Object,Pool Object )
连接池企业应用组件图如下:
类图如下:
主要文件代码:
/// <summary>
/// 连接池上下文
/// </summary>
public sealed class ConnectionPoolContext : IDisposable
{
/// <summary>
/// 池对象工厂
/// </summary>
private PoolObjectFactory _factory;
/// <summary>
/// 默认连接字符
/// </summary>
private string _defaultConnectionString;
/// <summary>
/// 工厂名称
/// </summary>
private string _factoryName;
/// <summary>
/// 类型
/// </summary>
private Type _factoryType;
/// <summary>
/// SQL状态仓库
/// </summary>
private SqlStatementRepository _statementRepository;
/// <summary>
/// 连接池
/// </summary>
private static Dictionary<string, ConnectionPoolContext> _instances = new Dictionary<string, ConnectionPoolContext>();
/// <summary>
/// 默认连接字符属性
/// </summary>
public string DefaultConnectionString
{
get
{
return _defaultConnectionString;
}
set
{
_defaultConnectionString = value;
}
}
/// <summary>
/// 工厂名
/// </summary>
public string FactoryName
{
get
{
return _factoryName;
}
set
{
_factoryName = value;
}
}
/// <summary>
/// SQL状态仓库属性
/// </summary>
public SqlStatementRepository StatementRepository
{
get
{
if (_statementRepository == null)
_statementRepository = new SqlStatementRepository();
return _statementRepository;
}
}
/// <summary>
/// 对象工厂模式
/// </summary>
internal PoolObjectFactory Factory
{
get
{
return _factory;
}
set
{
_factory = value;
}
}
/// <summary>
/// 实例化一个连接池
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static ConnectionPoolContext Instance(string id)
{
return _instances[id];
}
/// <summary>
/// 添加一个连接到池中
/// </summary>
/// <param name="id">id</param>
/// <param name="factoryName">工厂名称</param>
/// <param name="connectionString">连接字符串</param>
/// <param name="max">最大数</param>
/// <param name="limit">是否限制</param>
public static void AddPool(string id, string factoryName, string connectionString, int max, bool limit)
{
ConnectionPoolContext p = new ConnectionPoolContext();
p._defaultConnectionString = connectionString;
p._factoryName = factoryName;
p.Factory = new PoolObjectFactory(max, limit, new ArrayList());
_instances.Add(id, p);
}
/// <summary>
/// 得到一个连接
/// </summary>
/// <returns>数据库连接</returns>
public DbConnection AcquireConnection()
{
if (_factoryType == null)
{
DbProviderFactory dbFactory = DbProviderFactories.GetFactory(_factoryName);
DbConnection conn = dbFactory.CreateConnection();
_factoryType = conn.GetType();
}
DbConnection connection = (DbConnection)Factory.AcquireObject(_factoryType);
if (connection.ConnectionString == string.Empty)
connection.ConnectionString = _defaultConnectionString;
return connection;
}
/// <summary>
/// 释放一个连接
/// </summary>
/// <param name="conn">数据库连接</param>
public void ReleaseConnection(DbConnection conn)
{
Factory.ReleaseObject(conn);
conn.Close();
}
#region IDisposable Members
/// <summary>
/// 释放连接
/// </summary>
public void Dispose()
{
if (_factory != null)
((IDisposable)_factory).Dispose();
}
#endregion
/// <summary>
/// 私有构造函数
/// </summary>
private ConnectionPoolContext()
{
}
}