ConnectionPoolold.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Ant.ConnectionPool
  9. {
  10. public class ConnectionPoolOld
  11. {
  12. private static ConnectionPoolOld cpool = null;//池管理对象
  13. private static Object objlock = typeof(ConnectionPoolOld);//池管理对象实例
  14. private int size = 10;//池中连接数
  15. private int useCount = 0;//已经使用的连接数
  16. private ArrayList pool = null;//连接保存的集合
  17. private String ConnectionStr = "";//连接字符串
  18. public ConnectionPoolOld()
  19. {
  20. //
  21. // TODO: 在此处添加构造函数逻辑
  22. //
  23. ConnectionStr = "server=127.0.0.1;database=Chloe;uid=sa;pwd=a1b2c3d4";
  24. size = 100;
  25. pool = new ArrayList();
  26. }
  27. //创建获取连接池对象
  28. public static ConnectionPoolOld getPool()
  29. {
  30. lock (objlock)
  31. {
  32. if (cpool == null)
  33. {
  34. cpool = new ConnectionPoolOld();
  35. }
  36. return cpool;
  37. }
  38. }
  39. //获取池中的连接
  40. public SqlConnection getConnection()
  41. {
  42. lock (pool)
  43. {
  44. SqlConnection tmp = null;
  45. if (pool.Count > 0)
  46. {
  47. tmp = (SqlConnection)pool[0];
  48. pool.RemoveAt(0);
  49. //不成功
  50. if (!isUserful(tmp))
  51. {
  52. //可用的连接数据已去掉一个
  53. useCount--;
  54. tmp = getConnection();
  55. }
  56. }
  57. else
  58. {
  59. //可使用的连接小于连接数量
  60. if (useCount < size)
  61. {
  62. try
  63. {
  64. //创建连接
  65. SqlConnection conn = new SqlConnection(ConnectionStr);
  66. conn.Open();
  67. useCount++;
  68. tmp = conn;
  69. }
  70. catch (Exception e)
  71. {
  72. }
  73. }
  74. }
  75. return tmp;
  76. }
  77. }
  78. //关闭连接,加连接回到池中
  79. public void closeConnection(SqlConnection con)
  80. {
  81. lock (pool)
  82. {
  83. if (con != null)
  84. {
  85. pool.Add(con);
  86. }
  87. }
  88. }
  89. //目的保证所创连接成功,测试池中连接
  90. private bool isUserful(SqlConnection con)
  91. {
  92. //主要用于不同用户
  93. bool result = true;
  94. if (con != null)
  95. {
  96. string sql = "select 1";//随便执行对数据库操作
  97. SqlCommand cmd = new SqlCommand(sql, con);
  98. try
  99. {
  100. cmd.ExecuteScalar().ToString();
  101. }
  102. catch
  103. {
  104. result = false;
  105. }
  106. }
  107. return result;
  108. }
  109. }
  110. public static class testConnectionPoolOld
  111. {
  112. public static void Gettest()
  113. {
  114. //调用
  115. SqlConnection conn = null;
  116. for (int i = 1; i <= 100000; ++i)
  117. {
  118. //获取连接
  119. conn = ConnectionPoolOld.getPool().getConnection();
  120. try
  121. {
  122. //数据操作
  123. SqlCommand cmd = new SqlCommand("Select * from Users", conn);
  124. SqlDataReader dr = cmd.ExecuteReader();
  125. while (dr.Read())
  126. {
  127. Console.WriteLine("ID:" + i + ",姓名:" + dr[1]);
  128. }
  129. dr.Close();
  130. //将连接添加回连接池中
  131. ConnectionPoolOld.getPool().closeConnection(conn);
  132. }
  133. catch (Exception ex)
  134. {
  135. Console.WriteLine("\n异常信息:\n{0}", ex.Message);
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. }