C#數據庫連接池 MySql SqlServer


查閱了一天的資料來學習MySql數據庫連接池,終於在一篇博文上找到了,自己也整理了一下,希望對大家有用處

1. 建立連接池

  1 using MySql.Data.MySqlClient;
  2 using System;
  3 using System.Collections;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 
  9 namespace LianJieChiTest
 10 {
 11     public class ConnectionPool
 12     {
 13         private static ConnectionPool cpool = null;//池管理對象
 14         private static Object objlock = typeof(ConnectionPool);//池管理對象實例
 15         private int size = 1;//池中連接數
 16         private int useCount = 0;//已經使用的連接數
 17         private ArrayList pool = null;//連接保存的集合
 18         private String ConnectionStr = "";//連接字符串
 19 
 20         public ConnectionPool()
 21         {
 22             //數據庫連接字符串
 23             ConnectionStr = "server=localhost;User ID=root;Password=123456;database=test;";
 24             //創建可用連接的集合
 25             pool = new ArrayList();
 26         }
 27 
 28         #region 創建獲取連接池對象
 29         public static ConnectionPool getPool()
 30         {
 31             lock (objlock)
 32             {
 33                 if (cpool == null)
 34                 {
 35                     cpool = new ConnectionPool();
 36                 }
 37                 return cpool;
 38             }
 39         }
 40         #endregion
 41 
 42         #region 獲取池中的連接
 43         public MySqlConnection getConnection()
 44         {
 45             lock (pool)
 46             {
 47                 MySqlConnection tmp = null;
 48                 //可用連接數量大於0
 49                 if (pool.Count > 0)
 50                 {
 51                     //取第一個可用連接
 52                     tmp = (MySqlConnection)pool[0];
 53                     //在可用連接中移除此鏈接
 54                     pool.RemoveAt(0);
 55                     //不成功
 56                     if (!isUserful(tmp))
 57                     {
 58                         //可用的連接數據已去掉一個
 59                         useCount--;
 60                         tmp = getConnection();
 61                     }
 62                 }
 63                 else
 64                 {
 65                     //可使用的連接小於連接數量
 66                     if (useCount <= size)
 67                     {
 68                         try
 69                         {
 70                             //創建連接
 71                             tmp = CreateConnection(tmp);
 72                         }
 73                         catch (Exception e)
 74                         {
 75                         }
 76                     }
 77                 }
 78                 //連接為null
 79                 if (tmp == null)
 80                 {
 81                     //達到最大連接數遞歸調用獲取連接否則創建新連接
 82                     if (useCount <= size)
 83                     {
 84                         tmp = getConnection();
 85                     }
 86                     else
 87                     {
 88                         tmp = CreateConnection(tmp);
 89                     }
 90                 }
 91                 return tmp;
 92             }
 93         }
 94         #endregion
 95 
 96         #region 創建連接
 97         private MySqlConnection CreateConnection(MySqlConnection tmp)
 98         {
 99             //創建連接
100             MySqlConnection conn = new MySqlConnection(ConnectionStr);
101             conn.Open();
102             //可用的連接數加上一個
103             useCount++;
104             tmp = conn;
105             return tmp;
106         }
107         #endregion
108 
109         #region 關閉連接,加連接回到池中
110         public void closeConnection(MySqlConnection con)
111         {
112             lock (pool)
113             {
114                 if (con != null)
115                 {
116                     //將連接添加在連接池中
117                     pool.Add(con);
118                 }
119             }
120         }
121         #endregion
122 
123         #region 目的保證所創連接成功,測試池中連接
124         private bool isUserful(MySqlConnection con)
125         {
126             //主要用於不同用戶
127             bool result = true;
128             if (con != null)
129             {
130                 string sql = "select 1";//隨便執行對數據庫操作
131                 MySqlCommand cmd = new MySqlCommand(sql, con);
132                 try
133                 {
134                     cmd.ExecuteScalar().ToString();
135                 }
136                 catch
137                 {
138                     result = false;
139                 }
140 
141             }
142             return result;
143         }
144         #endregion
145     }
146 }
View Code

2. 使用

 1 MySqlConnection conn = null;
 2 for (int i = 1; i <= 100000; ++i)
 3 {
 4         //獲取連接
 5         conn = ConnectionPool.getPool().getConnection();
 6         try
 7         {
 8              //數據操作
 9              MySqlCommand cmd = new MySqlCommand("Select * from zhy_testLianJie", conn);
10              MySqlDataReader dr = cmd.ExecuteReader();
11              while (dr.Read())
12              {
13                   Console.WriteLine("ID:" + i + ",姓名:" + dr[1]);
14               }
15               dr.Close();
16               //將連接添加回連接池中
17               ConnectionPool.getPool().closeConnection(conn);
18         }
19         catch (Exception ex)
20         {
21            Console.WriteLine("\n異常信息:\n{0}", ex.Message);
22            break;
23         }
24 }                
View Code

這里是MySql的使用方法,SqlServer與之相差就是去掉所有對象的“My”,希望可以幫助到大家

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM