电子药箱通讯服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionFactory.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace RDH.Data
  10. {
  11. public class ConnectionFactory
  12. {
  13. #region Fields
  14. private TimeSpan _timeOut = new TimeSpan(0, 0, 0, 6);
  15. private AutoResetEvent _maxConnCountEvt = new AutoResetEvent(true);
  16. private int MAX_CONN_COUNT = 1;
  17. /// <summary>
  18. /// one session per thread ;
  19. /// </summary>
  20. private IDictionary<int, IDbConnection> _sessionCache;
  21. private static byte[] _locker = new byte[0];
  22. private static ConnectionFactory _current = null;
  23. #endregion
  24. public string ConnectionString { get; set; }
  25. private String _dbType;
  26. public static ConnectionFactory Current
  27. {
  28. get
  29. {
  30. lock (_locker)
  31. {
  32. if (_current == null)
  33. {
  34. _current = new ConnectionFactory();
  35. }
  36. return _current;
  37. }
  38. }
  39. }
  40. private ConnectionFactory()
  41. {
  42. _sessionCache = new Dictionary<int, IDbConnection>();
  43. _dbType = ConfigurationManager.AppSettings["DbType"] ?? "OracleClient";
  44. }
  45. public IDbConnection GetSessionConnection()
  46. {
  47. return GetSessionConnection(ConnectionString);
  48. }
  49. public IDbConnection GetSessionConnection(String connectionString)
  50. {
  51. if (_sessionCache.ContainsKey(Thread.CurrentThread.ManagedThreadId))
  52. {
  53. IDbConnection st = _sessionCache[Thread.CurrentThread.ManagedThreadId];
  54. return st;
  55. }
  56. else
  57. {
  58. if (_sessionCache.Count >= MAX_CONN_COUNT)
  59. {
  60. _maxConnCountEvt.WaitOne(_timeOut);
  61. }
  62. lock (this)
  63. {
  64. switch (_dbType)
  65. {
  66. default:
  67. case "OracleClient":
  68. _sessionCache.Add(Thread.CurrentThread.ManagedThreadId,
  69. new Oracle.ManagedDataAccess.Client.OracleConnection(connectionString));
  70. break;
  71. }
  72. return _sessionCache[Thread.CurrentThread.ManagedThreadId];
  73. }
  74. }
  75. }
  76. public void CloseSessionConnection()
  77. {
  78. if (_sessionCache.ContainsKey(Thread.CurrentThread.ManagedThreadId))
  79. {
  80. var session = _sessionCache[Thread.CurrentThread.ManagedThreadId];
  81. session.Close();
  82. session.Dispose();
  83. session = null;
  84. _maxConnCountEvt.Set();
  85. lock (this)
  86. {
  87. _sessionCache.Remove(Thread.CurrentThread.ManagedThreadId);
  88. }
  89. }
  90. else
  91. {
  92. //throw new Exception("Session Closed more than once , Or Session not open yet");
  93. }
  94. //if (_sessionCache.Count >= this.MAX_SESSION_COUNT)
  95. //{
  96. // var toRemove = _sessionCache.Where(a => a.Value.LastChangedTime + _timeOut < DateTime.Now).Select(b => b.Key);
  97. // foreach (var key in toRemove)
  98. // {
  99. // _sessionCache.Remove(key);
  100. // }
  101. //}
  102. }
  103. }
  104. }