2 // Mono.Data.TdsClient.TdsConnectionPool.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
6 // Christian Hergert (christian.hergert@gmail.com)
7 // Gonzalo Paniagua Javier (gonzalo@novell.com)
9 // Copyright (C) 2004 Novell, Inc.
10 // Copyright (C) 2009 Novell, Inc.
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 using System.Threading;
38 namespace Mono.Data.Tds.Protocol
40 public class TdsConnectionPoolManager
42 Hashtable pools = Hashtable.Synchronized (new Hashtable ());
45 public TdsConnectionPoolManager (TdsVersion version)
47 this.version = version;
50 public TdsConnectionPool GetConnectionPool (string connectionString, TdsConnectionInfo info)
52 TdsConnectionPool pool = (TdsConnectionPool) pools [connectionString];
54 pools [connectionString] = new TdsConnectionPool (this, info);
55 pool = (TdsConnectionPool) pools [connectionString];
60 public TdsConnectionPool GetConnectionPool (string connectionString)
62 return (TdsConnectionPool) pools [connectionString];
65 public virtual Tds CreateConnection (TdsConnectionInfo info)
69 case TdsVersion.tds42:
70 return new Tds42 (info.DataSource, info.Port, info.PacketSize, info.Timeout);
71 case TdsVersion.tds50:
72 return new Tds50 (info.DataSource, info.Port, info.PacketSize, info.Timeout);
73 case TdsVersion.tds70:
74 return new Tds70 (info.DataSource, info.Port, info.PacketSize, info.Timeout);
75 case TdsVersion.tds80:
76 return new Tds80 (info.DataSource, info.Port, info.PacketSize, info.Timeout);
78 throw new NotSupportedException ();
81 public IDictionary GetConnectionPool ()
87 public class TdsConnectionInfo
89 public TdsConnectionInfo (string dataSource, int port, int packetSize, int timeout, int minSize, int maxSize)
91 DataSource = dataSource;
93 PacketSize = packetSize;
95 PoolMinSize = minSize;
96 PoolMaxSize = maxSize;
99 public string DataSource;
101 public int PacketSize;
103 public int PoolMinSize;
104 public int PoolMaxSize;
106 public override string ToString ()
108 StringBuilder sb = new StringBuilder ();
109 sb.AppendFormat ("DataSouce: {0}\n", DataSource);
110 sb.AppendFormat ("Port: {0}\n", Port);
111 sb.AppendFormat ("PacketSize: {0}\n", PacketSize);
112 sb.AppendFormat ("Timeout: {0}\n", Timeout);
113 sb.AppendFormat ("PoolMinSize: {0}\n", PoolMinSize);
114 sb.AppendFormat ("PoolMaxSize: {0}", PoolMaxSize);
115 return sb.ToString ();
119 public class TdsConnectionPool
121 TdsConnectionInfo info;
123 TdsConnectionPoolManager manager;
127 public TdsConnectionPool (TdsConnectionPoolManager manager, TdsConnectionInfo info)
130 this.manager = manager;
131 conns = new ArrayList (info.PoolMaxSize);
132 available = new Queue (info.PoolMaxSize);
136 void InitializePool ()
138 /* conns.Count might not be 0 when we are resetting the connection pool */
139 for (int i = conns.Count; i < info.PoolMinSize; i++) {
141 Tds t = manager.CreateConnection (info);
143 available.Enqueue (t);
145 // Ignore. GetConnection will throw again.
150 public bool Pooling {
151 get { return !no_pooling; }
152 set { no_pooling = !value; }
158 public Tds GetConnection ()
161 return manager.CreateConnection (info);
165 int retries = info.PoolMaxSize * 2;
167 while (result == null) {
170 if (available.Count > 0) {
171 result = (Tds) available.Dequeue ();
172 break; // .. and do the reset out of the loop
174 Monitor.Enter (conns);
176 if (conns.Count >= info.PoolMaxSize - in_progress) {
177 Monitor.Exit (conns);
178 bool got_lock = Monitor.Wait (available, info.Timeout * 1000);
180 throw new InvalidOperationException (
181 "Timeout expired. The timeout period elapsed before a " +
182 "connection could be obtained. A possible explanation " +
183 "is that all the connections in the pool are in use, " +
184 "and the maximum pool size is reached.");
185 } else if (available.Count > 0) {
186 result = (Tds) available.Dequeue ();
187 break; // .. and do the reset out of the loop
195 Monitor.Exit (conns); // Exiting if not owned is ok < 2.x
200 result = manager.CreateConnection (info);
211 bool remove_cnc = true;
212 Exception exc = null;
214 remove_cnc = (!result.IsConnected || !result.Reset ());
215 } catch (Exception e) {
221 conns.Remove (result);
222 result.Disconnect ();
231 public void ReleaseConnection (Tds connection)
233 if (connection == null)
236 connection.Disconnect ();
240 if (connection.poolStatus == 2) {
242 conns.Remove (connection);
243 connection.Disconnect ();
247 if (connection != null) // connection is still open
248 available.Enqueue (connection);
249 // We pulse even if we don't queue, because null means that there's a slot
250 // available in 'conns'
251 Monitor.Pulse (available);
256 public void ResetConnectionPool ()
262 for (i = conns.Count - 1; i >= 0; i++) {
263 tds = (Tds) conns [i];
264 tds.poolStatus = 2; // 2 -> disconnect me upon release
266 for (i = available.Count - 1; i >= 0; i--) {
267 tds = (Tds) available.Dequeue ();
274 Monitor.PulseAll (available);
278 #endregion // Methods