2 // Mono.Data.TdsClient.TdsConnectionPool.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) 2004 Novell, Inc.
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using Mono
.Data
.Tds
.Protocol
;
33 using System
.Collections
;
34 using System
.Threading
;
36 namespace Mono
.Data
.Tds
.Protocol
38 public class TdsConnectionPoolManager
40 Hashtable pools
= new Hashtable ();
43 public TdsConnectionPoolManager (TdsVersion version
)
45 this.version
= version
;
48 public TdsConnectionPool
GetConnectionPool (string connectionString
, TdsConnectionInfo info
)
52 TdsConnectionPool pool
= (TdsConnectionPool
) pools
[connectionString
];
54 pool
= new TdsConnectionPool (this, info
);
55 pools
[connectionString
] = pool
;
61 public virtual ITds
CreateConnection (TdsConnectionInfo info
)
65 case TdsVersion
.tds42
:
66 return new Tds42 (info
.DataSource
, info
.Port
, info
.PacketSize
, info
.Timeout
);
67 case TdsVersion
.tds50
:
68 return new Tds50 (info
.DataSource
, info
.Port
, info
.PacketSize
, info
.Timeout
);
69 case TdsVersion
.tds70
:
70 return new Tds70 (info
.DataSource
, info
.Port
, info
.PacketSize
, info
.Timeout
);
71 case TdsVersion
.tds80
:
72 return new Tds80 (info
.DataSource
, info
.Port
, info
.PacketSize
, info
.Timeout
);
74 throw new NotSupportedException ();
78 public class TdsConnectionInfo
80 public TdsConnectionInfo (string dataSource
, int port
, int packetSize
, int timeout
, int minSize
, int maxSize
)
82 DataSource
= dataSource
;
84 PacketSize
= packetSize
;
86 PoolMinSize
= minSize
;
87 PoolMaxSize
= maxSize
;
90 public string DataSource
;
92 public int PacketSize
;
94 public int PoolMinSize
;
95 public int PoolMaxSize
;
98 public class TdsConnectionPool
100 ArrayList list
= new ArrayList ();
101 TdsConnectionInfo info
;
103 int activeConnections
= 0;
104 TdsConnectionPoolManager manager
;
106 public TdsConnectionPool (TdsConnectionPoolManager manager
, TdsConnectionInfo info
)
109 this.manager
= manager
;
114 public ITds
GetConnection ()
116 ITds connection
= null;
122 for (int n
=0; n
<info
.PoolMinSize
; n
++)
123 list
.Add (CreateConnection ());
130 // There are available connections
131 connection
= (ITds
) list
[list
.Count
- 1];
132 list
.RemoveAt (list
.Count
- 1);
133 if (!connection
.Reset ()) {
139 if (connection
== null && activeConnections
< info
.PoolMaxSize
)
141 // No connections available, but the connection limit
142 // has not been reached yet, so a new one can be created
143 connection
= CreateConnection();
146 // No available connections in the pool
147 // Wait for somewone to release one.
148 if (connection
== null)
153 while (connection
== null);
159 public void ReleaseConnection (ITds tds
)
164 Monitor
.Pulse (list
);
168 ITds
CreateConnection ()
171 return manager
.CreateConnection (info
);
174 #endregion // Methods