OleDbConnectionFactory.cs 6.5 KB

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