??????? 本文是用.net實(shí)現(xiàn)的數(shù)據(jù)庫(kù)鏈接池,大體體現(xiàn)了數(shù)據(jù)庫(kù)鏈接池的實(shí)現(xiàn)思想,ADO.NET已經(jīng)提供了很好的鏈接池維護(hù),所以本程序基本沒(méi)有什么實(shí)用價(jià)值:

          1.鏈接池管理類
          using System;
          using System.Data;
          using System.Data.SqlClient;
          using System.Data.OleDb;
          using System.Collections;
          using System.Configuration;

          /// <summary>
          /// DbConnectionPool 的摘要說(shuō)明
          /// </summary>

          namespace dbopr
          {
          ??? public class DbConnectionPool
          ??? {
          ??????? //定義存放數(shù)據(jù)庫(kù)鏈接的隊(duì)列
          ??????? private static Queue connections=new Queue();

          ??????? //數(shù)據(jù)庫(kù)參數(shù)
          ??????? private static string ConnString = "";??????????????????????????????????? //鏈接字符串

          ??????? //鏈接池參數(shù)
          ??????? private static int InitSize = 20;???????????????????????????????????????? //初始化鏈接池大小
          ??????? private static int MaxSize = 100;???????????????????????????????????????? //最大鏈接數(shù)
          ??????? private static int QueueSize = 50;??????????????????????????????????????? //隊(duì)列中最大維護(hù)的鏈接數(shù)
          ??????? private static int ConnNum = 0;?????????????????????????????????????????? //系統(tǒng)維護(hù)的鏈接總數(shù)


          ??????? //定義數(shù)據(jù)庫(kù)類型,1表示為SqlServer數(shù)據(jù)庫(kù),2表示其它的OLE DB;默認(rèn)為SqlServer
          ??????? private static int? DbType = 1;
          ??????? public static int DbType
          ??????? {
          ??????????? set
          ??????????? {
          ??????????????? DbType = value;
          ??????????? }
          ??????? }

          ??????? private DbConnectionPool()
          ??????? {
          ??????????? try{
          ??????????????? ConnString = Configuration.connectionStrings["DbSource"].connectionString;
          ??????????????? InitSize = int.Parse(Configuration.connectionStrings["InitSize"].connectionString);
          ??????????????? MaxSize = int.Parse(Configuration.connectionStrings["MaxSize"].connectionString);
          ??????????????? QueueSize = int.Parse(Configuration.connectionStrings["QueueSize"].connectionString);
          ??????????????? DbType = int.Parse(Configuration.connectionStrings["DbType"].connectionString);
          ??????????????? initPool();
          ??????????? }
          ??????????? catch(Exception e) {
          ??????????? }
          ??????? }
          ???????
          ??????? //初始化鏈接池
          ??????? private static void initPool(){
          ??????????? int i;
          ??????????? for (i = 1; i <= InitSize; i++) {
          ??????????????? if (DbType == 1)
          ??????????????????? connections.Enqueue(newSqlConn());
          ??????????????? else
          ??????????????????? connections.Enqueue(newOleConn());
          ??????????? }
          ??????? }

          ??????? //獲取一個(gè)Sql Server鏈接對(duì)象
          ??????? private static SqlConnection newSqlConn()
          ??????? {
          ??????????? ConnNum++;
          ??????????? return new SqlConnection(ConnString);
          ??????? }
          ???????
          ??????? //獲取一個(gè)OLE DB鏈接對(duì)像
          ??????? private static OleDbConnection newOleConn()
          ??????? {
          ??????????? ConnNum++;
          ??????????? return new OleDbConnection(ConnString);
          ??????? }

          ??????? //獲取一個(gè)數(shù)據(jù)庫(kù)鏈接
          ??????? public static IDbConnection getConn() {
          ??????????? if (connections.Count == 0)????????????????????????????????? //如果隊(duì)列中鏈接用完,則新建一個(gè)鏈接放入隊(duì)列
          ??????????? {
          ??????????????? if (ConnNum > MaxSize)?????????????????????????????????? //如果當(dāng)前活動(dòng)鏈接達(dá)到最大,則等待鏈接
          ??????????????????? return null;
          ??????????????? else
          ??????????????? {
          ??????????????????? if (DbType == 1)
          ??????????????????????? connections.Enqueue(newSqlConn());
          ??????????????????? else
          ??????????????????????? connections.Enqueue(newOleConn());
          ??????????????????? return connections.Dequeue();
          ??????????????? }
          ??????????? }
          ??????????? else
          ??????????????? return connections.Dequeue();
          ??????? }

          ??????? //釋放一個(gè)活運(yùn)鏈接
          ??????? public static void reConn(IDbConnection conn) {
          ??????????? if (connections.Count > QueueSize)?????????????????????????? //如果隊(duì)列中元素個(gè)數(shù)已達(dá)到最大,則關(guān)閉鏈接
          ??????????????? conn.Close();
          ??????????? else
          ??????????????? connections.Enqueue(conn);
          ??????? }

          ??????? //銷毀鏈接池
          ??????? public static void ClearPool() {
          ??????????? while(connections.Count>0)
          ???????????????? (IDbConnection)connections.Dequeue().Close();
          ??????? }
          ??? }
          }

          2.鏈接生成類
          using System;
          using System.Data;
          using System.Data.SqlClient;
          using System.Data.OleDb;
          using System.Configuration;
          using System.Web;

          /// <summary>
          /// DbConn 的摘要說(shuō)明
          /// </summary>

          namespace dbopr
          {
          ??? public class DbConnection
          ??? {
          ??????? private static IDbConnection conn;
          ???????
          ??????? public DbConnection()
          ??????? {
          ??????????? //
          ??????????? // TODO: 在此處添加構(gòu)造函數(shù)邏輯
          ??????????? //
          ??????? }

          ??????? public static SqlConnection getSqlConn()
          ??????? {
          ??????????? //獲取鏈接池中的Sql Server的鏈接
          ??????????? conn = DbConnectionPool.getConn();
          ??????????? return (SqlConnection)conn;
          ??????? }

          ??????? public static OleDbConnection getOleConn() {
          ??????????? //獲取其它OLE DB的鏈接
          ??????????? conn = DbConnectionPool.getConn();
          ??????????? return (OleDbConnection)conn;
          ??????? }

          ??????? public static void Close() {
          ??????????? //放回用過(guò)的鏈接
          ??????????? DbConnectionPool.reConn(conn);
          ??????? }
          ??? }
          }

          posted on 2006-12-01 17:11 WindDC 閱讀(527) 評(píng)論(0)  編輯  收藏 所屬分類: .net

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 吉首市| 丰都县| 襄城县| 西藏| 桂东县| 孝昌县| 呼和浩特市| 巍山| 防城港市| 琼结县| 旬邑县| 吴堡县| 施甸县| 兴文县| 平乐县| 岳池县| 明溪县| 通化市| 运城市| 滦南县| 博乐市| 光山县| 祁连县| 许昌市| 九江市| 高陵县| 宿州市| 军事| 竹溪县| 隆子县| 临汾市| 广丰县| 西安市| 辽阳县| 青岛市| 大足县| 清原| 周至县| 天柱县| 镇坪县| 镇远县|