SqlConnectionPool.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Data.SqlClient.SqlConnectionPool.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) 2002 Tim Coleman
  8. //
  9. using Mono.Data.TdsClient.Internal;
  10. using System;
  11. using System.Collections;
  12. using System.Threading;
  13. namespace System.Data.SqlClient {
  14. internal class SqlConnectionPool : MarshalByRefObject, IList, ICollection, IEnumerable
  15. {
  16. #region Fields
  17. ArrayList list = new ArrayList ();
  18. int maxSize;
  19. int minSize;
  20. int packetSize;
  21. int port;
  22. string dataSource;
  23. #endregion // Fields
  24. #region Constructors
  25. public SqlConnectionPool (string dataSource, int port, int packetSize, int minSize, int maxSize)
  26. {
  27. this.dataSource = dataSource;
  28. this.port = port;
  29. this.packetSize = packetSize;
  30. this.minSize = minSize;
  31. this.maxSize = maxSize;
  32. }
  33. #endregion // Constructors
  34. #region Properties
  35. public Tds this[int index] {
  36. get { return (Tds) list[index]; }
  37. }
  38. object IList.this[int index] {
  39. get { return this[index]; }
  40. set { throw new InvalidOperationException (); }
  41. }
  42. public int Count {
  43. get { return list.Count; }
  44. }
  45. public bool IsFixedSize {
  46. get { return false; }
  47. }
  48. public bool IsReadOnly {
  49. get { return true; }
  50. }
  51. public bool IsSynchronized {
  52. get { return false; }
  53. }
  54. public int MaxSize {
  55. get { return maxSize; }
  56. }
  57. public int MinSize {
  58. get { return minSize; }
  59. }
  60. public object SyncRoot {
  61. get { throw new InvalidOperationException (); }
  62. }
  63. #endregion // Properties
  64. #region Methods
  65. public int Add (object o)
  66. {
  67. return list.Add ((Tds) o);
  68. }
  69. public void Clear ()
  70. {
  71. list.Clear ();
  72. }
  73. public bool Contains (object o)
  74. {
  75. return list.Contains ((Tds) o);
  76. }
  77. public void CopyTo (Array array, int index)
  78. {
  79. list.CopyTo (array, index);
  80. }
  81. public IEnumerator GetEnumerator ()
  82. {
  83. return list.GetEnumerator ();
  84. }
  85. [MonoTODO]
  86. public ITds AllocateConnection ()
  87. {
  88. // make sure we have the minimum count (really only useful the first time)
  89. lock (list) {
  90. for (int i = Count; i < minSize; i += 1)
  91. Add (new Tds70 (dataSource, port, packetSize));
  92. }
  93. // Try to obtain a lock
  94. foreach (object o in list)
  95. if (Monitor.TryEnter (o))
  96. return (ITds) o;
  97. if (Count < maxSize) {
  98. Tds tds = new Tds70 (dataSource, port, packetSize);
  99. Monitor.Enter (tds);
  100. Add (tds);
  101. return tds;
  102. }
  103. // else we have to wait for one to be available
  104. return null;
  105. }
  106. public void ReleaseConnection (ITds tds)
  107. {
  108. Monitor.Exit (tds);
  109. }
  110. public int IndexOf (object o)
  111. {
  112. return list.IndexOf ((Tds) o);
  113. }
  114. public void Insert (int index, object o)
  115. {
  116. list.Insert (index, (Tds) o);
  117. }
  118. public void Remove (object o)
  119. {
  120. list.Remove ((Tds) o);
  121. }
  122. public void RemoveAt (int index)
  123. {
  124. list.RemoveAt (index);
  125. }
  126. #endregion // Methods
  127. }
  128. }