2 // Mono.Data.TdsClient.TdsConnectionPool.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) 2002 Tim Coleman
10 using Mono.Data.TdsClient.Internal;
12 using System.Collections;
13 using System.Threading;
15 namespace Mono.Data.TdsClient {
16 internal class TdsConnectionPool : MarshalByRefObject, IList, ICollection, IEnumerable
20 ArrayList list = new ArrayList ();
33 public TdsConnectionPool (string dataSource, int port, int packetSize, int minSize, int maxSize)
35 this.dataSource = dataSource;
37 this.packetSize = packetSize;
38 this.minSize = minSize;
39 this.maxSize = maxSize;
42 #endregion // Constructors
46 public Tds this[int index] {
47 get { return (Tds) list[index]; }
50 object IList.this[int index] {
51 get { return this[index]; }
52 set { throw new InvalidOperationException (); }
56 get { return list.Count; }
59 public bool IsFixedSize {
63 public bool IsReadOnly {
67 public bool IsSynchronized {
72 get { return maxSize; }
76 get { return minSize; }
79 public object SyncRoot {
80 get { throw new InvalidOperationException (); }
83 #endregion // Properties
87 public int Add (object o)
89 return list.Add ((Tds) o);
97 public bool Contains (object o)
99 return list.Contains ((Tds) o);
102 public void CopyTo (Array array, int index)
104 list.CopyTo (array, index);
107 public IEnumerator GetEnumerator ()
109 return list.GetEnumerator ();
113 public ITds AllocateConnection ()
115 // make sure we have the minimum count (really only useful the first time)
117 for (int i = Count; i < minSize; i += 1)
118 Add (new Tds42 (dataSource, port, packetSize));
121 // Try to obtain a lock
122 foreach (object o in list)
123 if (Monitor.TryEnter (o))
126 if (Count < maxSize) {
127 Tds tds = new Tds42 (dataSource, port, packetSize);
133 // else we have to wait for one to be available
138 public void ReleaseConnection (ITds tds)
143 public int IndexOf (object o)
145 return list.IndexOf ((Tds) o);
148 public void Insert (int index, object o)
150 list.Insert (index, (Tds) o);
153 public void Remove (object o)
155 list.Remove ((Tds) o);
158 public void RemoveAt (int index)
160 list.RemoveAt (index);
163 #endregion // Methods