AAA.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.Common;
  6. namespace DateBaseConnectionPool
  7. {
  8. /// <summary>
  9. /// 连接池状态
  10. /// </summary>
  11. public enum PoolState
  12. {
  13. /// <summary>
  14. /// 刚刚创建的对象,表示该对象未被调用过StartSeivice方法。
  15. /// </summary>
  16. UnInitialize,
  17. /// <summary>
  18. /// 初始化中,该状态下服务正在按照参数初始化连接池。
  19. /// </summary>
  20. Initialize,
  21. /// <summary>
  22. /// 运行中
  23. /// </summary>
  24. Run,
  25. /// <summary>
  26. /// 停止状态
  27. /// </summary>
  28. Stop
  29. }
  30. /// <summary>
  31. /// 要申请连接的级别
  32. /// </summary>
  33. public enum ConnLevel
  34. {
  35. /// <summary>
  36. /// 独占方式,分配全新的连接资源,并且该连接资源在本次使用释放回连接池之前不能在分配出去。如果连接池只能分配引用记数类型连接资源则该级别将产生一个异常,标志连接池资源耗尽
  37. /// </summary>
  38. ReadOnly,
  39. /// <summary>
  40. /// 优先级-高,分配全新的连接资源,不使用引用记数技术。注:此级别不保证在分配后该连接资源后,仍然保持独立占有资源,若想独立占有资源请使用ReadOnely
  41. /// </summary>
  42. High,
  43. /// <summary>
  44. /// 优先级-中,适当应用引用记数技术分配连接
  45. /// </summary>
  46. None,
  47. /// <summary>
  48. /// 优先级-底,尽可能使用引用记数技术分配连接
  49. /// </summary>
  50. Bottom
  51. }
  52. /// <summary>
  53. /// 连接类型
  54. /// </summary>
  55. public enum ConnTypeEnum
  56. {
  57. /// <summary>
  58. /// ODBC 数据源
  59. /// </summary>
  60. Odbc,
  61. /// <summary>
  62. /// OLE DB 数据源
  63. /// </summary>
  64. OleDb,
  65. /// <summary>
  66. /// SqlServer 数据库连接
  67. /// </summary>
  68. SqlClient,
  69. /// <summary>
  70. /// 默认(无分配)
  71. /// </summary>
  72. None
  73. }
  74. /// <summary>
  75. /// 连接池中的一个连接类型
  76. /// </summary>
  77. public class ConnStruct : IDisposable
  78. {
  79. /// <summary>
  80. /// 连接池中的连接
  81. /// </summary>
  82. /// <param name="dbc">数据库连接</param>
  83. /// <param name="cte">连接类型</param>
  84. public ConnStruct(DbConnection dbc, ConnTypeEnum cte)
  85. {
  86. createTime = DateTime.Now;
  87. connect = dbc;
  88. connType = cte;
  89. }
  90. /// <summary>
  91. /// 连接池中的连接
  92. /// </summary>
  93. /// <param name="dt">连接创建时间</param>
  94. /// <param name="dbc">数据库连接</param>
  95. /// <param name="cte">连接类型</param>
  96. public ConnStruct(DbConnection dbc, ConnTypeEnum cte, DateTime dt)
  97. {
  98. createTime = dt;
  99. connect = dbc;
  100. connType = cte;
  101. }
  102. //--------------------------------------------------------------------
  103. private bool enable = true;//是否失效
  104. private bool use = false;//是否正在被使用中
  105. private bool allot = true;//表示该连接是否可以被分配
  106. private DateTime createTime = DateTime.Now;//创建时间
  107. private int useDegree = 0;//被使用次数
  108. private int repeatNow = 0;//当前连接被重复引用多少
  109. private bool isRepeat = true;//连接是否可以被重复引用,当被分配出去的连接可能使用事务时,该属性被标识为true
  110. private ConnTypeEnum connType = ConnTypeEnum.None;//连接类型
  111. private DbConnection connect = null;//连接对象
  112. private object obj = null;//连接附带的信息
  113. #region 属性部分
  114. /// <summary>
  115. /// 表示该连接是否可以被分配
  116. /// </summary>
  117. public bool Allot
  118. {
  119. get { return allot; }
  120. set { allot = value; }
  121. }
  122. /// <summary>
  123. /// 是否失效;false表示失效,只读
  124. /// </summary>
  125. public bool Enable
  126. { get { return enable; } }
  127. /// <summary>
  128. /// 是否正在被使用中,只读
  129. /// </summary>
  130. public bool IsUse
  131. { get { return use; } }
  132. /// <summary>
  133. /// 创建时间,只读
  134. /// </summary>
  135. public DateTime CreateTime
  136. { get { return createTime; } }
  137. /// <summary>
  138. /// 被使用次数,只读
  139. /// </summary>
  140. public int UseDegree
  141. { get { return useDegree; } }
  142. /// <summary>
  143. /// 当前连接被重复引用多少,只读
  144. /// </summary>
  145. public int RepeatNow
  146. { get { return repeatNow; } }
  147. /// <summary>
  148. /// 得到数据库连接状态,只读
  149. /// </summary>
  150. public ConnectionState State
  151. { get { return connect.State; } }
  152. /// <summary>
  153. /// 得到该连接,只读
  154. /// </summary>
  155. public DbConnection Connection
  156. { get { return connect; } }
  157. /// <summary>
  158. /// 连接是否可以被重复引用
  159. /// </summary>
  160. public bool IsRepeat
  161. {
  162. get { return isRepeat; }
  163. set { isRepeat = value; }
  164. }
  165. /// <summary>
  166. /// 连接类型,只读
  167. /// </summary>
  168. public ConnTypeEnum ConnType
  169. { get { return connType; } }
  170. /// <summary>
  171. /// 连接附带的信息
  172. /// </summary>
  173. public object Obj
  174. {
  175. get { return obj; }
  176. set { obj = value; }
  177. }
  178. #endregion
  179. /// <summary>
  180. /// 打开数据库连接
  181. /// </summary>
  182. public void Open()
  183. { connect.Open(); }
  184. /// <summary>
  185. /// 关闭数据库连接
  186. /// </summary>
  187. public void Close()
  188. { connect.Close(); }
  189. /// <summary>
  190. /// 无条件将连接设置为失效
  191. /// </summary>
  192. public void SetConnectionLost()
  193. { enable = false; allot = false; }
  194. /// <summary>
  195. /// 被分配出去,线程安全的
  196. /// </summary>
  197. public void Repeat()
  198. {
  199. lock (this)
  200. {
  201. if (enable == false)//连接可用
  202. throw new ResLostnExecption();//连接资源已经失效
  203. if (allot == false)//是否可以被分配
  204. throw new AllotExecption();//连接资源不可以被分配
  205. if (use == true && isRepeat == false)
  206. throw new AllotAndRepeatExecption();//连接资源已经被分配并且不允许重复引用
  207. repeatNow++;//引用记数+1
  208. useDegree++;//被使用次数+1
  209. use = true;//被使用
  210. }
  211. }
  212. /// <summary>
  213. /// 被释放回来,线程安全的
  214. /// </summary>
  215. public void Remove()
  216. {
  217. lock (this)
  218. {
  219. if (enable == false)//连接可用
  220. throw new ResLostnExecption();//连接资源已经失效
  221. if (repeatNow == 0)
  222. throw new RepeatIsZeroExecption();//引用记数已经为0
  223. repeatNow--;//引用记数-1
  224. if (repeatNow == 0)
  225. use = false;//未使用
  226. else
  227. use = true;//使用中
  228. }
  229. }
  230. /// <summary>
  231. /// 释放资源
  232. /// </summary>
  233. public void Dispose()
  234. {
  235. enable = false;
  236. connect.Close();
  237. connect = null;
  238. }
  239. }
  240. }