62,046
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 连接对象。
/// </summary>
public class Pool
{
/// <summary>
/// 初始化。
/// </summary>
/// <param name="id">代码。</param>
/// <param name="connection">数据库连接对象。</param>
/// <param name="isUse">使用标志。</param>
/// <param name="openTime">打开时间。</param>
public Pool(Guid id, IDbConnection connection, bool isUse, DateTime openTime)
{
this.id = id;
this.connection = connection;
IsUse = isUse;
OpenTime = openTime;
}
private Guid id;
/// <summary>
/// 获取连接对象的代码。
/// </summary>
public Guid Id { get { return id; } }
private IDbConnection connection;
/// <summary>
/// 获取数据库连接对象。
/// </summary>
public IDbConnection Connection { get { return connection; } }
/// <summary>
/// 获取或设置一个值,该值指示连接对象是否已经使用,true-已经使用,否则没有使用。
/// </summary>
public bool IsUse { get; set; }
/// <summary>
/// 获取或设置连接对象打开数据库的时间。
/// </summary>
public DateTime OpenTime { get; set; }
}
/// <summary>
/// 连接池管理类。
/// </summary>
public static class ConnectionPool
{
private static List<Pool> pools = new List<Pool>();
private static int poolTimeout = 300;
private static int maxPoolSize = 200;
private static string connectString = "";
private static bool getParam = false;
static ConnectionPool()
{
}
/// <summary>
/// 获取参数。
/// </summary>
/// <param name="connectionString">配置文件中的数据库连接字符串。</param>
private static void GetParams(string connectionString)
{
string[] connectStrings = connectionString.Split(';');
StringBuilder newConnectString = new StringBuilder();
foreach (string subConnectString in connectStrings)
{
if (subConnectString.IndexOf("Provider", StringComparison.InvariantCultureIgnoreCase) != -1 ||
subConnectString.IndexOf("Data Source", StringComparison.InvariantCultureIgnoreCase) != -1)
{
newConnectString.Append(subConnectString);
newConnectString.Append(";");
}
if (subConnectString.IndexOf("Max Pool Size", StringComparison.InvariantCultureIgnoreCase) != -1)
{
string[] poolSizeses = subConnectString.Split('=');
maxPoolSize = int.Parse(poolSizeses[1]);
}
if (subConnectString.IndexOf("Connect Timeout", StringComparison.InvariantCultureIgnoreCase) != -1)
{
string[] timeouts = subConnectString.Split('=');
poolTimeout = int.Parse(timeouts[1]);
}
}
connectString = newConnectString.ToString();
getParam = true;
}
/// <summary>
/// 根据数据库类型创建数据库。
/// </summary>
/// <param name="connectType">数据库连接类型。</param>
/// <returns>指定数据库连接类型的数据库连接对象。</returns>
private static IDbConnection CreateConnection(ConnectionType connectType)
{
switch (connectType)
{
case ConnectionType.OleConnectionType:
return new OleDbConnection(connectString);
default:
throw new Exception("此数据库类型不能使用此连接池。");
}
}
/// <summary>
/// 根据数据库连接字符串及数据库类型创建连接对象。
/// </summary>
/// <param name="connectionString">数据库连接字符串。</param>
/// <param name="connectType">数据库类型。</param>
/// <returns>连接对象。</returns>
public static Pool Open(string connectionString, ConnectionType connectType)
{
lock (typeof(ConnectionPool))
{
if (!getParam)
{
GetParams(connectionString);
}
foreach (Pool pool in pools)
{
if (!pool.IsUse)
{
pool.IsUse = true;
pool.OpenTime = DateTime.Now;
return pool;
}
}
if (pools.Count >= maxPoolSize)
{
throw new Exception("连接池的连接数已经超过最大值,不能再提供数据库连接。");
}
Pool newPool = new Pool(Guid.NewGuid(), CreateConnection(connectType), true, DateTime.Now);
pools.Add(newPool);
newPool.Connection.Open();
return newPool;
}
}
/// <summary>
/// 关闭数据库连接。
/// </summary>
/// <param name="closePool">需要关闭的连接池对象。</param>
public static void Close(Pool closePool)
{
for (int num = pools.Count - 1; num >= 0; num--)
{
if (pools[num].Id == closePool.Id)
{
pools[num].IsUse = false;
}
else
{
if (pools[num].IsUse)
{
continue;
}
TimeSpan time = DateTime.Now - pools[num].OpenTime;
if (time.TotalSeconds > poolTimeout)
{
pools[num].Connection.Close();
pools.Remove(pools[num]);
}
}
}
}
}
HTML xmlns:IE>
<HEAD>
<STYLE>
@media all {
IE\:CLIENTCAPS {behavior:url(#default#clientCaps)}
}
</STYLE>
<SCRIPT>
function window.onload()
{
sTempStr = "availHeight = " + oClientCaps.availHeight + "\n" +
"availWidth = " + oClientCaps.availWidth + "\n" +
"bufferDepth = " + oClientCaps.bufferDepth + "\n" +
"colorDepth = " + oClientCaps.colorDepth + "\n" +
"connectionType = " + oClientCaps.connectionType + "\n" +
"cookieEnabled = " + oClientCaps.cookieEnabled + "\n" +
"cpuClass = " + oClientCaps.cpuClass + "\n" +
"height = " + oClientCaps.height + "\n" +
"javaEnabled = " + oClientCaps.javaEnabled + "\n" +
"platform = " + oClientCaps.platform + "\n" +
"systemLanguage = " + oClientCaps.systemLanguage + "\n" +
"userLanguage = " + oClientCaps.userLanguage + "\n" +
"width = " + oClientCaps.width + "\n" ;
oPre.innerText = sTempStr;
}
</SCRIPT>
</HEAD>
<BODY>
<H1>clientCaps Behavior Sample</H1>
<P>This example shows how to use the new <B>clientCaps</B>
behavior, introduced in Microsoft Internet Explorer 5, to obtain
client capabilities information. The following is a sampling of the
information that can be obtained:</P>
<IE:CLIENTCAPS ID="oClientCaps" />
<PRE id="oPre"></PRE>
</BODY>
</HTML>