DbConnectionManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Common;
  6. using System.Web;
  7. using System.Configuration;
  8. using System.Data.SqlClient;
  9. namespace Bowin.Common.Linq.DB
  10. {
  11. internal static class ConnectionCache
  12. {
  13. static ConnectionCache()
  14. {
  15. IsGlobalConnectionStarted = false;
  16. ConnectionContexts = new Dictionary<string, DbConnection>();
  17. }
  18. public static bool IsGlobalConnectionStarted { get; set; }
  19. public static Dictionary<string, DbConnection> ConnectionContexts { get; set; }
  20. }
  21. public abstract class DbConnectionManager
  22. {
  23. public static bool IsGlobalConnectionStarted
  24. {
  25. get
  26. {
  27. if (HttpContext.Current != null && HttpContext.Current.Session != null)
  28. {
  29. return (bool)(HttpContext.Current.Session["IsGlobalConnectionStarted"] ?? false);
  30. }
  31. else
  32. {
  33. return ConnectionCache.IsGlobalConnectionStarted;
  34. }
  35. }
  36. set
  37. {
  38. if (HttpContext.Current != null)
  39. {
  40. HttpContext.Current.Session["IsGlobalConnectionStarted"] = value;
  41. }
  42. else
  43. {
  44. ConnectionCache.IsGlobalConnectionStarted = value;
  45. }
  46. }
  47. }
  48. protected static bool ExistsConnection(string contextKey)
  49. {
  50. if (HttpContext.Current != null)
  51. {
  52. return HttpContext.Current.Session[contextKey] != null;
  53. }
  54. else
  55. {
  56. return ConnectionCache.ConnectionContexts[contextKey] != null;
  57. }
  58. }
  59. public static void AddConnection(string contextKey, DbConnection db)
  60. {
  61. if (HttpContext.Current != null)
  62. {
  63. HttpContext.Current.Session[contextKey] = db;
  64. }
  65. else
  66. {
  67. ConnectionCache.ConnectionContexts.Add(contextKey, db);
  68. }
  69. }
  70. public static void CloseConnections()
  71. {
  72. if (HttpContext.Current != null)
  73. {
  74. foreach (ConnectionStringSettings connectionSetting in System.Configuration.ConfigurationManager.ConnectionStrings)
  75. {
  76. DbConnection db;
  77. db = (DbConnection)HttpContext.Current.Session[connectionSetting.Name];
  78. if (db != null)
  79. {
  80. if (db.State != System.Data.ConnectionState.Closed)
  81. {
  82. db.Close();
  83. }
  84. }
  85. }
  86. }
  87. else
  88. {
  89. foreach (var connection in ConnectionCache.ConnectionContexts)
  90. {
  91. if (connection.Value.State != System.Data.ConnectionState.Closed)
  92. {
  93. connection.Value.Close();
  94. }
  95. }
  96. }
  97. }
  98. public static void ClearConnection()
  99. {
  100. if (HttpContext.Current != null)
  101. {
  102. foreach (ConnectionStringSettings connectionSetting in System.Configuration.ConfigurationManager.ConnectionStrings)
  103. {
  104. HttpContext.Current.Session[connectionSetting.Name] = null;
  105. }
  106. }
  107. else
  108. {
  109. ConnectionCache.ConnectionContexts = new Dictionary<string, DbConnection>();
  110. }
  111. IsGlobalConnectionStarted = false;
  112. }
  113. internal static DbConnection GetConnection(string contextKey)
  114. {
  115. if (HttpContext.Current != null)
  116. {
  117. return (DbConnection)HttpContext.Current.Session[contextKey];
  118. }
  119. else
  120. {
  121. return ConnectionCache.ConnectionContexts[contextKey];
  122. }
  123. }
  124. }
  125. }