DbConnectionManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public static void AddConnection(string contextKey, DbConnection db)
  49. {
  50. if (HttpContext.Current != null)
  51. {
  52. HttpContext.Current.Session[contextKey] = db;
  53. }
  54. else
  55. {
  56. ConnectionCache.ConnectionContexts.Add(contextKey, db);
  57. }
  58. }
  59. public static void CloseConnections()
  60. {
  61. if (HttpContext.Current != null)
  62. {
  63. foreach (ConnectionStringSettings connectionSetting in System.Configuration.ConfigurationManager.ConnectionStrings)
  64. {
  65. DbConnection db;
  66. db = (DbConnection)HttpContext.Current.Session[connectionSetting.Name];
  67. if (db != null)
  68. {
  69. if (db.State != System.Data.ConnectionState.Closed)
  70. {
  71. db.Close();
  72. }
  73. }
  74. }
  75. }
  76. else
  77. {
  78. foreach (var connection in ConnectionCache.ConnectionContexts)
  79. {
  80. if (connection.Value.State != System.Data.ConnectionState.Closed)
  81. {
  82. connection.Value.Close();
  83. }
  84. }
  85. }
  86. }
  87. public static void ClearConnection()
  88. {
  89. if (HttpContext.Current != null)
  90. {
  91. foreach (ConnectionStringSettings connectionSetting in System.Configuration.ConfigurationManager.ConnectionStrings)
  92. {
  93. HttpContext.Current.Session[connectionSetting.Name] = null;
  94. }
  95. }
  96. else
  97. {
  98. ConnectionCache.ConnectionContexts = new Dictionary<string, DbConnection>();
  99. }
  100. IsGlobalConnectionStarted = false;
  101. }
  102. internal static DbConnection GetConnection(string contextKey)
  103. {
  104. if (HttpContext.Current != null)
  105. {
  106. return (DbConnection)HttpContext.Current.Session[contextKey];
  107. }
  108. else
  109. {
  110. return ConnectionCache.ConnectionContexts[contextKey];
  111. }
  112. }
  113. }
  114. }