对象池及数据库连接对象池
呵呵,也不是什么新东西,最后由代码下载。先看张图
下面是代码,有注释
objectPool类:
最后,代码下载:/Files/luoboqingcai/objectPool.rar
最最后,发个萝卜激动语录:(最后一次发,哈哈) 没钱,就别玩大学这场游戏
下面是代码,有注释
objectPool类:
//
// 对象池
//
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Collections;
namespace objectPool
{
/// <summary>
/// 对象池
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
abstract class objectPool<T>
{
/// <summary>
/// 最后取出对象时间
/// </summary>
private long longLastCheckOut = 0;
/// <summary>
/// 记录当前正在使用对象Hashtable
/// </summary>
private static Hashtable objLocked;
/// <summary>
/// 记录全部空闲及可用对象Hashtable
/// </summary>
private static Hashtable objUnLocked;
/// <summary>
/// 检查所有空闲及可用对象时间间隔:60秒(用于垃圾回收)
/// </summary>
private static long CLEANINTERVAL = 60 * 1000;
/// <summary>
/// 初始化成员变量
/// </summary>
static objectPool()
{
// 实例化对象
objLocked = Hashtable.Synchronized(new Hashtable());
objUnLocked = Hashtable.Synchronized(new Hashtable());
}
/// <summary>
/// 创建一个新的对象
/// </summary>
/// <returns>对象实例</returns>
protected abstract T Create();
/// <summary>
/// 检查特定对象有效性
/// </summary>
/// <param name="t">对象实例</param>
/// <returns>是否有效 true or false</returns>
protected abstract bool ValiDate(T t);
/// <summary>
/// 删除对象
/// </summary>
/// <param name="t">对象实例</param>
protected abstract void Expire(T t);
/// <summary>
/// 构造函数
/// </summary>
internal objectPool()
{
// 取得现在时间
longLastCheckOut = DateTime.Now.Ticks;
// 实例化一个Timer对象
Timer timer = new Timer();
// 设置时间间隔
timer.Interval = CLEANINTERVAL;
timer.Enabled = true;
// 到达时间间隔发生(垃圾回收)
timer.Elapsed += delegate
{
lock (this)
{
long longNow = DateTime.Now.Ticks;
try
{
foreach (DictionaryEntry entry in objUnLocked)
{
T t = (T)entry.Key;
// 超出垃圾回收时间间隔
if (longNow - (long)objUnLocked[t] > CLEANINTERVAL)
{
// 从可用对象中排除
objUnLocked.Remove(t);
// 删除对象
Expire(t);
}
}
}
catch (Exception)
{ }
}
};
}
/// <summary>
/// 从池中获取对象
/// </summary>
/// <returns>对象实例</returns>
internal T GetObjectFromPool()
{
long longNow = DateTime.Now.Ticks;
longLastCheckOut = longNow;
T t;
lock (this)
{
try
{
foreach (DictionaryEntry entry in objUnLocked)
{
t = (T)entry.Key;
// 如果对象有效
if (ValiDate(t))
{
// 设置为非空闲对象
objUnLocked.Remove(t);
objLocked.Add(t, longNow);
return t;
}
else
{
// 删除对象
objUnLocked.Remove(t);
Expire(t);
}
}
}
catch (Exception)
{ }
// 新创建对象
t = Create();
// 设置为正在使用对象
objLocked.Add(t, longNow);
}
return t;
}
/// <summary>
/// 将对象放入池中
/// </summary>
/// <param name="t">对象实例</param>
internal void ReturnObjectToPool(T t)
{
if (!object.Equals(null, t))
{
lock (this)
{
// 设置对象为空闲对象
objLocked.Remove(t);
objUnLocked.Add(t, DateTime.Now.Ticks);
}
}
}
}
}
DBConn类:// 对象池
//
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Collections;
namespace objectPool
{
/// <summary>
/// 对象池
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
abstract class objectPool<T>
{
/// <summary>
/// 最后取出对象时间
/// </summary>
private long longLastCheckOut = 0;
/// <summary>
/// 记录当前正在使用对象Hashtable
/// </summary>
private static Hashtable objLocked;
/// <summary>
/// 记录全部空闲及可用对象Hashtable
/// </summary>
private static Hashtable objUnLocked;
/// <summary>
/// 检查所有空闲及可用对象时间间隔:60秒(用于垃圾回收)
/// </summary>
private static long CLEANINTERVAL = 60 * 1000;
/// <summary>
/// 初始化成员变量
/// </summary>
static objectPool()
{
// 实例化对象
objLocked = Hashtable.Synchronized(new Hashtable());
objUnLocked = Hashtable.Synchronized(new Hashtable());
}
/// <summary>
/// 创建一个新的对象
/// </summary>
/// <returns>对象实例</returns>
protected abstract T Create();
/// <summary>
/// 检查特定对象有效性
/// </summary>
/// <param name="t">对象实例</param>
/// <returns>是否有效 true or false</returns>
protected abstract bool ValiDate(T t);
/// <summary>
/// 删除对象
/// </summary>
/// <param name="t">对象实例</param>
protected abstract void Expire(T t);
/// <summary>
/// 构造函数
/// </summary>
internal objectPool()
{
// 取得现在时间
longLastCheckOut = DateTime.Now.Ticks;
// 实例化一个Timer对象
Timer timer = new Timer();
// 设置时间间隔
timer.Interval = CLEANINTERVAL;
timer.Enabled = true;
// 到达时间间隔发生(垃圾回收)
timer.Elapsed += delegate
{
lock (this)
{
long longNow = DateTime.Now.Ticks;
try
{
foreach (DictionaryEntry entry in objUnLocked)
{
T t = (T)entry.Key;
// 超出垃圾回收时间间隔
if (longNow - (long)objUnLocked[t] > CLEANINTERVAL)
{
// 从可用对象中排除
objUnLocked.Remove(t);
// 删除对象
Expire(t);
}
}
}
catch (Exception)
{ }
}
};
}
/// <summary>
/// 从池中获取对象
/// </summary>
/// <returns>对象实例</returns>
internal T GetObjectFromPool()
{
long longNow = DateTime.Now.Ticks;
longLastCheckOut = longNow;
T t;
lock (this)
{
try
{
foreach (DictionaryEntry entry in objUnLocked)
{
t = (T)entry.Key;
// 如果对象有效
if (ValiDate(t))
{
// 设置为非空闲对象
objUnLocked.Remove(t);
objLocked.Add(t, longNow);
return t;
}
else
{
// 删除对象
objUnLocked.Remove(t);
Expire(t);
}
}
}
catch (Exception)
{ }
// 新创建对象
t = Create();
// 设置为正在使用对象
objLocked.Add(t, longNow);
}
return t;
}
/// <summary>
/// 将对象放入池中
/// </summary>
/// <param name="t">对象实例</param>
internal void ReturnObjectToPool(T t)
{
if (!object.Equals(null, t))
{
lock (this)
{
// 设置对象为空闲对象
objLocked.Remove(t);
objUnLocked.Add(t, DateTime.Now.Ticks);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace objectPool
{
/// <summary>
/// 数据库连接对象池
/// </summary>
class DBConn : objectPool<SqlConnection>
{
/// <summary>
/// 私有构造函数
/// </summary>
private DBConn()
{ }
/// <summary>
/// 返回自身对象(Singleton)
/// </summary>
public static readonly DBConn Instance = new DBConn();
/// <summary>
/// 默认连接语句
/// </summary>
private static string strConnStr = @"Data Source=WORK0249\;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=sa";
/// <summary>
/// 连接语句(属性)
/// </summary>
public string StrConnStr
{
get
{
return strConnStr;
}
set
{
strConnStr = value;
}
}
/// <summary>
/// 创建数据库连接对象
/// </summary>
/// <returns>已打开的数据库连接对象</returns>
protected override SqlConnection Create()
{
SqlConnection conn = new SqlConnection(strConnStr);
conn.Open();
return conn;
}
/// <summary>
/// 验证连接状态
/// </summary>
/// <param name="t">数据库连接对象</param>
/// <returns>连接状态Open : true Close:false</returns>
protected override bool ValiDate(SqlConnection t)
{
try
{
return !(t.State.Equals(ConnectionState.Closed));
}
catch (SqlException)
{
return false;
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
/// <param name="t">数据库连接对象</param>
protected override void Expire(SqlConnection t)
{
try
{
t.Close();
}
catch (SqlException)
{ }
}
/// <summary>
/// 从池中获取数据库连接
/// </summary>
/// <returns>数据库连接对象</returns>
public SqlConnection BorrowDBConn()
{
try
{
return base.GetObjectFromPool();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将连接对象放入池中
/// </summary>
/// <param name="conn">数据库连接对象</param>
public void ReturnDBConn(SqlConnection conn)
{
base.ReturnObjectToPool(conn);
}
}
}
使用示例:using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace objectPool
{
/// <summary>
/// 数据库连接对象池
/// </summary>
class DBConn : objectPool<SqlConnection>
{
/// <summary>
/// 私有构造函数
/// </summary>
private DBConn()
{ }
/// <summary>
/// 返回自身对象(Singleton)
/// </summary>
public static readonly DBConn Instance = new DBConn();
/// <summary>
/// 默认连接语句
/// </summary>
private static string strConnStr = @"Data Source=WORK0249\;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=sa";
/// <summary>
/// 连接语句(属性)
/// </summary>
public string StrConnStr
{
get
{
return strConnStr;
}
set
{
strConnStr = value;
}
}
/// <summary>
/// 创建数据库连接对象
/// </summary>
/// <returns>已打开的数据库连接对象</returns>
protected override SqlConnection Create()
{
SqlConnection conn = new SqlConnection(strConnStr);
conn.Open();
return conn;
}
/// <summary>
/// 验证连接状态
/// </summary>
/// <param name="t">数据库连接对象</param>
/// <returns>连接状态Open : true Close:false</returns>
protected override bool ValiDate(SqlConnection t)
{
try
{
return !(t.State.Equals(ConnectionState.Closed));
}
catch (SqlException)
{
return false;
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
/// <param name="t">数据库连接对象</param>
protected override void Expire(SqlConnection t)
{
try
{
t.Close();
}
catch (SqlException)
{ }
}
/// <summary>
/// 从池中获取数据库连接
/// </summary>
/// <returns>数据库连接对象</returns>
public SqlConnection BorrowDBConn()
{
try
{
return base.GetObjectFromPool();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将连接对象放入池中
/// </summary>
/// <param name="conn">数据库连接对象</param>
public void ReturnDBConn(SqlConnection conn)
{
base.ReturnObjectToPool(conn);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace objectPool
{
class Program
{
static void Main(string[] args)
{
DBConn dbconn = DBConn.Instance;
// 从池中取出对象
SqlConnection conn = dbconn.BorrowDBConn();
string strSql = "SELECT AddressID, AddressLine1, AddressLine2, City, StateProvinceID, PostalCode, " +
"rowguid, ModifiedDate FROM Person.Address";
SqlCommand command = new SqlCommand(strSql, conn);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.Write(reader[0]);
}
// 将对象放入池中
dbconn.ReturnDBConn(conn);
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace objectPool
{
class Program
{
static void Main(string[] args)
{
DBConn dbconn = DBConn.Instance;
// 从池中取出对象
SqlConnection conn = dbconn.BorrowDBConn();
string strSql = "SELECT AddressID, AddressLine1, AddressLine2, City, StateProvinceID, PostalCode, " +
"rowguid, ModifiedDate FROM Person.Address";
SqlCommand command = new SqlCommand(strSql, conn);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.Write(reader[0]);
}
// 将对象放入池中
dbconn.ReturnDBConn(conn);
Console.Read();
}
}
}
最后,代码下载:/Files/luoboqingcai/objectPool.rar
最最后,发个萝卜激动语录:(最后一次发,哈哈) 没钱,就别玩大学这场游戏