码迷,mamicode.com
首页 > 数据库 > 详细

C#数据库连接池 MySql SqlServer

时间:2014-12-16 18:56:54      阅读:3102      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   使用   sp   

查阅了一天的资料来学习MySql数据库连接池,终于在一遍博文上找到了,自己也整理了一下,希望对大家有用处

1. 建立线程池

bubuko.com,布布扣
  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 = 100;//池中连接数
 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=zhy;Password=123456;database=lianjietest;";
 24             //创建可用连接的集合
 25             pool = new ArrayList();
 26         }
 27 
 28         //创建获取连接池对象
 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         //获取池中的连接
 41         public MySqlConnection getConnection()
 42         {
 43             lock (pool)
 44             {
 45                 MySqlConnection tmp = null;
 46                 //可用连接数量大于0
 47                 if (pool.Count > 0)
 48                 {
 49                     //取第一个可用连接
 50                     tmp = (MySqlConnection)pool[0];
 51                     tmp.Open();
 52                     //在可用连接中移除此链接
 53                     pool.RemoveAt(0);
 54                     //不成功
 55                     if (!isUserful(tmp))
 56                     {
 57                         //可用的连接数据已去掉一个
 58                         useCount--;
 59                         tmp = getConnection();
 60                     }
 61                 }
 62                 else
 63                 {
 64                     //可使用的连接小于连接数量
 65                     if (useCount <= size)
 66                     {
 67                         try
 68                         {
 69                             //创建连接
 70                             MySqlConnection conn = new MySqlConnection(ConnectionStr);
 71                             conn.Open();
 72                             //可用的连接数加上一个
 73                             useCount++;
 74                             tmp = conn;
 75                         }
 76                         catch (Exception e)
 77                         {
 78                         }
 79                     }
 80                 }
 81                 return tmp;
 82             }
 83         }
 84         //关闭连接,加连接回到池中
 85         public void closeConnection(MySqlConnection con)
 86         {
 87             lock (pool)
 88             {
 89                 if (con != null)
 90                 {
 91                     //将连接添加在连接池中
 92                     pool.Add(con);
 93                 }
 94             }
 95         }
 96         //目的保证所创连接成功,测试池中连接
 97         private bool isUserful(MySqlConnection con)
 98         {
 99             //主要用于不同用户
100             bool result = true;
101             if (con != null)
102             {
103                 string sql = "select 1";//随便执行对数据库操作
104                 MySqlCommand cmd = new MySqlCommand(sql, con);
105                 try
106                 {
107                     cmd.ExecuteScalar().ToString();
108                 }
109                 catch
110                 {
111                     result = false;
112                 }
113 
114             }
115             return result;
116         }
117     }
118 }
View Code

2. 使用

bubuko.com,布布扣
 1 MySqlConnection conn = null;
 2             for (int i = 1; i <= 100; ++i)
 3             {
 4                 //获取连接
 5                 conn = ConnectionPool.getPool().getConnection();
 6                 try
 7                 {
 8                     //连接为null等待空闲连接
 9                     while (conn == null)
10                     {
11                     }
12                     //数据操作
13                     MySqlCommand cmd = new MySqlCommand("Select * from zhy_testLianJie", conn);
14                     MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
15                     while (dr.Read())
16                     {
17                         Console.WriteLine("ID:" + dr[0] + ",姓名:" + dr[1]);
18                     }
19                     //关闭连接
20                     conn.Close();
21                     //将连接添加回连接池中
22                     ConnectionPool.getPool().closeConnection(conn);
23                 }
24                 catch (Exception ex)
25                 {
26                     Console.WriteLine("\n异常信息:\n{0}", ex.Message);
27                     break;
28                 }
29             }
View Code

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

 

C#数据库连接池 MySql SqlServer

标签:style   blog   http   ar   io   color   os   使用   sp   

原文地址:http://www.cnblogs.com/zhhying/p/4167614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!