1. 程式人生 > 實用技巧 >Access資料庫連線池

Access資料庫連線池

Access資料庫沒有資料庫連線池,每次開啟和關閉資料庫時都非常耗時,自己編寫了一個簡易的資料庫連線池,新增資料庫型別還可支援其他沒有資料庫連線池的資料庫型別。

該資料庫連線池要求必須更改web.config中的資料庫連線字串,新增Max Pool Size及Connect Timeout兩個屬性,如:<add name="AccessConnectString" connectionString="”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:"db.accdb;Max Pool Size=200;Connect Timeout=300"
/>;,以獲取需要最大的連線數量及連線物件生存期。 該資料庫連線池是在關閉資料庫連線,檢驗其他連線物件的生存期,這樣在沒有發出關閉資料庫連線的請求時,一些已經超過生存期連線物件仍然在記憶體中,還無法做到與其他資料庫連線池同樣的效果。 資料庫連線池程式碼如下: /// <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]); } } } } } 呼叫程式碼: private connectionString = ConfigurationManager.ConnectionStrings[“AccessConnectString”].ToString(); //獲取連線物件 private Pool pool=ConnectionPool.Open(connectionString, ConnectionType.OleConnectionType); //獲取資料庫連線物件 IDbConnection dbConnection= pool.Connection; 資料庫操作 //關閉資料庫連線 ConnectionPool.Close(pool);