zoukankan      html  css  js  c++  java
  • C#连接池

    查阅了一天的资料来学习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 }
    复制代码

    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("
    异常信息:
    {0}", ex.Message);
    22            break;
    23         }
    24 }                
    复制代码

    这里是MySql的使用方法,SqlServer与之相差就是去掉所有对象的“My”,希望可以帮助到大家

  • 相关阅读:
    智慧城市顶层设计策略方案(PPT)
    ant build.xml 解释!
    Excel poi API基础教程!
    操纵Excel文件的 ExcelUtil 类 !
    在selenium测试中使用XPATH功能函数starts-with、contains、descendant、ancestor、text()定位网页元素
    [ Selenium2 从零开始 by Bruce from http://seleniumcn.cn ] 1-8 视频集锦
    selenium 概念及练习 !
    selenium Object Page 设计模式理解及实现!
    使用TestNG 和 CSV文件进行数据驱动
    如何让评审人爱上我
  • 原文地址:https://www.cnblogs.com/developer-ios/p/10800791.html
Copyright © 2011-2022 走看看