OleDbConnectionFactory.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2002-2004 Mainsoft Corporation.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. using System;
  23. using System.Data;
  24. using System.Data.Common;
  25. using System.Reflection;
  26. using System.Collections;
  27. namespace System.Data.OleDb
  28. {
  29. internal class OleDbConnectionFactory
  30. {
  31. private static Object LockObj = new Object();
  32. private static Hashtable dataSourceCache = new Hashtable();
  33. private static Type OracleConnectionCacheImplType = Type.GetType("oracle.jdbc.pool.OracleConnectionCacheImpl");
  34. private static MethodInfo setUrlMethod = OracleConnectionCacheImplType.GetMethod("setURL", BindingFlags.Public | BindingFlags.Instance);
  35. private static MethodInfo setUserMethod = OracleConnectionCacheImplType.GetMethod("setUser", BindingFlags.Public | BindingFlags.Instance);
  36. private static MethodInfo setPasswordMethod = OracleConnectionCacheImplType.GetMethod("setPassword", BindingFlags.Public | BindingFlags.Instance);
  37. private static MethodInfo getActiveSizeMethod = OracleConnectionCacheImplType.GetMethod("getActiveSize", BindingFlags.Public | BindingFlags.Instance);
  38. private static MethodInfo closeMethod = OracleConnectionCacheImplType.GetMethod("close", BindingFlags.Public | BindingFlags.Instance);
  39. private static MethodInfo setMaxLimitMethod = OracleConnectionCacheImplType.GetMethod("setMaxLimit", BindingFlags.Public | BindingFlags.Instance);
  40. private static MethodInfo setMinLimitMethod = OracleConnectionCacheImplType.GetMethod("setMinLimit", BindingFlags.Public | BindingFlags.Instance);
  41. private static long timestamp = DateTime.Now.Ticks;
  42. private static int MINUTES_TIMEOUT = 60;
  43. private static DbStringManager StringManager = new DbStringManager("System.Data.System.Data.ProviderBase.jvm.OleDbStrings");
  44. internal static java.sql.Connection GetConnection(OleDbConnection.PROVIDER_TYPE providerType,String url,String user,String password,int timeout)
  45. {
  46. CacheKey key = new CacheKey(url,user,password);
  47. Object dataSourceObj;
  48. lock(LockObj)
  49. {
  50. if (TimeIsOver())
  51. Clear();
  52. dataSourceObj = dataSourceCache[key];
  53. if (dataSourceObj == null)
  54. {
  55. switch(providerType)
  56. {
  57. case OleDbConnection.PROVIDER_TYPE.MSDAORA :
  58. dataSourceObj = Activator.CreateInstance(OracleConnectionCacheImplType);
  59. Object[] setUrlArgs = new Object[1] { url };
  60. Object[] setUserArgs = new Object[1] { user };
  61. Object[] setPasswordArgs = new Object[1] { password };
  62. setUrlMethod.Invoke(dataSourceObj,setUrlArgs);
  63. setUserMethod.Invoke(dataSourceObj,setUserArgs);
  64. setPasswordMethod.Invoke(dataSourceObj,setPasswordArgs);
  65. int maxLimit = Convert.ToInt32(StringManager.GetString("ORA_CONNECTION_POOLING_MAX_LIMIT","-1"));
  66. int minLimit = Convert.ToInt32(StringManager.GetString("ORA_CONNECTION_POOLING_MIN_LIMIT","-1"));
  67. if(minLimit != -1)
  68. setMinLimitMethod.Invoke(dataSourceObj,new Object[1] { minLimit } );
  69. if(maxLimit != -1)
  70. setMaxLimitMethod.Invoke(dataSourceObj,new Object[1] { maxLimit } );
  71. break;
  72. }
  73. dataSourceCache.Add(key,dataSourceObj);
  74. }
  75. }
  76. java.sql.Connection conn;
  77. lock(dataSourceObj) {
  78. if (((javax.sql.DataSource)dataSourceObj).getLoginTimeout() != timeout) {
  79. ((javax.sql.DataSource)dataSourceObj).setLoginTimeout(timeout);
  80. }
  81. conn = ((javax.sql.DataSource)dataSourceObj).getConnection();
  82. }
  83. return conn;
  84. }
  85. private static bool TimeIsOver()
  86. {
  87. return (DateTime.Now.Ticks - timestamp > MINUTES_TIMEOUT * TimeSpan.TicksPerMinute);
  88. }
  89. private static void Clear()
  90. {
  91. ArrayList closedDataSources = new ArrayList();
  92. // collect all the datasources with no connections in use
  93. foreach(DictionaryEntry e in dataSourceCache)
  94. {
  95. Object dataSourceObj = e.Value;
  96. Object dataSourceObjKey = e.Key;
  97. if (getActiveSizeMethod.Invoke(dataSourceObj,new object[0]).Equals(0)) {
  98. closeMethod.Invoke(dataSourceObj,new object[0]);
  99. closedDataSources.Add(dataSourceObjKey);
  100. }
  101. }
  102. // close and remove all data sources with no connections in use
  103. foreach(Object dataSourceObjKey in closedDataSources) {
  104. dataSourceCache.Remove(dataSourceObjKey);
  105. }
  106. }
  107. }
  108. // Key for data sources cache
  109. // data sources mapped by jdbc url, user and password
  110. internal class CacheKey
  111. {
  112. private String url;
  113. private String user;
  114. private String password;
  115. public CacheKey(String url, String user, String password)
  116. {
  117. this.url = url;
  118. this.user = user;
  119. this.password = password;
  120. }
  121. public bool equals(Object o)
  122. {
  123. if (this == o) return true;
  124. if (!(o is CacheKey)) return false;
  125. CacheKey cacheKey = (CacheKey) o;
  126. if (!password.Equals(cacheKey.password)) return false;
  127. if (!url.Equals(cacheKey.url)) return false;
  128. if (!user.Equals(cacheKey.user)) return false;
  129. return true;
  130. }
  131. public int hashCode()
  132. {
  133. int result;
  134. result = url.GetHashCode();
  135. result = 29 * result + user.GetHashCode();
  136. result = 29 * result + password.GetHashCode();
  137. return result;
  138. }
  139. }
  140. }