OleDbConnectionFactory.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.Data.ProviderBase;
  34. using System.Reflection;
  35. using System.Collections;
  36. namespace System.Data.OleDb
  37. {
  38. internal class OleDbConnectionFactory
  39. {
  40. private static Object LockObj = new Object();
  41. private static Hashtable dataSourceCache = new Hashtable();
  42. private static Type OracleConnectionCacheImplType = Type.GetType("oracle.jdbc.pool.OracleConnectionCacheImpl");
  43. private static MethodInfo setUrlMethod = OracleConnectionCacheImplType.GetMethod("setURL", BindingFlags.Public | BindingFlags.Instance);
  44. private static MethodInfo setUserMethod = OracleConnectionCacheImplType.GetMethod("setUser", BindingFlags.Public | BindingFlags.Instance);
  45. private static MethodInfo setPasswordMethod = OracleConnectionCacheImplType.GetMethod("setPassword", BindingFlags.Public | BindingFlags.Instance);
  46. private static MethodInfo getActiveSizeMethod = OracleConnectionCacheImplType.GetMethod("getActiveSize", BindingFlags.Public | BindingFlags.Instance);
  47. private static MethodInfo closeMethod = OracleConnectionCacheImplType.GetMethod("close", BindingFlags.Public | BindingFlags.Instance);
  48. private static MethodInfo setMaxLimitMethod = OracleConnectionCacheImplType.GetMethod("setMaxLimit", BindingFlags.Public | BindingFlags.Instance);
  49. private static MethodInfo setMinLimitMethod = OracleConnectionCacheImplType.GetMethod("setMinLimit", BindingFlags.Public | BindingFlags.Instance);
  50. private static long timestamp = DateTime.Now.Ticks;
  51. private static int MINUTES_TIMEOUT = 60;
  52. private static DbStringManager StringManager = new DbStringManager("System.Data.System.Data.ProviderBase.jvm.OleDbStrings");
  53. internal static java.sql.Connection GetConnection(OleDbConnection.PROVIDER_TYPE providerType,String url,String user,String password,int timeout)
  54. {
  55. CacheKey key = new CacheKey(url,user,password);
  56. Object dataSourceObj;
  57. lock(LockObj)
  58. {
  59. if (TimeIsOver())
  60. Clear();
  61. dataSourceObj = dataSourceCache[key];
  62. if (dataSourceObj == null)
  63. {
  64. switch(providerType)
  65. {
  66. case OleDbConnection.PROVIDER_TYPE.MSDAORA :
  67. dataSourceObj = Activator.CreateInstance(OracleConnectionCacheImplType);
  68. Object[] setUrlArgs = new Object[1] { url };
  69. Object[] setUserArgs = new Object[1] { user };
  70. Object[] setPasswordArgs = new Object[1] { password };
  71. setUrlMethod.Invoke(dataSourceObj,setUrlArgs);
  72. setUserMethod.Invoke(dataSourceObj,setUserArgs);
  73. setPasswordMethod.Invoke(dataSourceObj,setPasswordArgs);
  74. int maxLimit = Convert.ToInt32(StringManager.GetString("ORA_CONNECTION_POOLING_MAX_LIMIT","-1"));
  75. int minLimit = Convert.ToInt32(StringManager.GetString("ORA_CONNECTION_POOLING_MIN_LIMIT","-1"));
  76. if(minLimit != -1)
  77. setMinLimitMethod.Invoke(dataSourceObj,new Object[1] { minLimit } );
  78. if(maxLimit != -1)
  79. setMaxLimitMethod.Invoke(dataSourceObj,new Object[1] { maxLimit } );
  80. break;
  81. }
  82. dataSourceCache.Add(key,dataSourceObj);
  83. }
  84. }
  85. java.sql.Connection conn;
  86. lock(dataSourceObj) {
  87. if (((javax.sql.DataSource)dataSourceObj).getLoginTimeout() != timeout) {
  88. ((javax.sql.DataSource)dataSourceObj).setLoginTimeout(timeout);
  89. }
  90. conn = ((javax.sql.DataSource)dataSourceObj).getConnection();
  91. }
  92. return conn;
  93. }
  94. private static bool TimeIsOver()
  95. {
  96. return (DateTime.Now.Ticks - timestamp > MINUTES_TIMEOUT * TimeSpan.TicksPerMinute);
  97. }
  98. private static void Clear()
  99. {
  100. ArrayList closedDataSources = new ArrayList();
  101. // collect all the datasources with no connections in use
  102. foreach(DictionaryEntry e in dataSourceCache)
  103. {
  104. Object dataSourceObj = e.Value;
  105. Object dataSourceObjKey = e.Key;
  106. if (getActiveSizeMethod.Invoke(dataSourceObj,new object[0]).Equals(0)) {
  107. closeMethod.Invoke(dataSourceObj,new object[0]);
  108. closedDataSources.Add(dataSourceObjKey);
  109. }
  110. }
  111. // close and remove all data sources with no connections in use
  112. foreach(Object dataSourceObjKey in closedDataSources) {
  113. dataSourceCache.Remove(dataSourceObjKey);
  114. }
  115. }
  116. }
  117. // Key for data sources cache
  118. // data sources mapped by jdbc url, user and password
  119. internal class CacheKey
  120. {
  121. private String url;
  122. private String user;
  123. private String password;
  124. public CacheKey(String url, String user, String password)
  125. {
  126. this.url = url;
  127. this.user = user;
  128. this.password = password;
  129. }
  130. public bool equals(Object o)
  131. {
  132. if (this == o) return true;
  133. if (!(o is CacheKey)) return false;
  134. CacheKey cacheKey = (CacheKey) o;
  135. if (!password.Equals(cacheKey.password)) return false;
  136. if (!url.Equals(cacheKey.url)) return false;
  137. if (!user.Equals(cacheKey.user)) return false;
  138. return true;
  139. }
  140. public int hashCode()
  141. {
  142. int result;
  143. result = url.GetHashCode();
  144. result = 29 * result + user.GetHashCode();
  145. result = 29 * result + password.GetHashCode();
  146. return result;
  147. }
  148. }
  149. }