MySql - fixed problem where socket was not getting closed properly (thanks Steve!)
[mono.git] / mcs / class / ByteFX.Data / mysqlclient / MySqlPoolManager.cs
1 using System;
2 using ByteFX.Data.Common;
3 using System.Collections;
4
5 namespace ByteFX.Data.MySqlClient
6 {
7         /// <summary>
8         /// Summary description for MySqlPoolManager.
9         /// </summary>
10         internal sealed class MySqlPoolManager
11         {
12                 private static Hashtable        pools;
13
14                 public MySqlPoolManager() 
15                 {
16                 }
17
18                 /// <summary>
19                 /// 
20                 /// </summary>
21                 private static void Initialize()
22                 {
23                         pools = new Hashtable();
24                 }
25
26                 public static MySqlInternalConnection GetConnection( MySqlConnectionString settings ) 
27                 {
28                         // make sure the manager is initialized
29                         if (MySqlPoolManager.pools == null)
30                                 MySqlPoolManager.Initialize();
31
32                         string text = settings.ConnectString;
33
34                         lock( pools.SyncRoot ) 
35                         {
36                                 MySqlPool pool;
37                                 if (!pools.Contains( text )) 
38                                 {
39                                         pool = new MySqlPool( settings.MinPoolSize, settings.MaxPoolSize );
40                                         pools.Add( text, pool );
41                                 }
42                                 else 
43                                 {
44                                         pool = (pools[text] as MySqlPool);
45                                 }
46
47                                 return pool.GetConnection( settings );
48                         }
49                 }
50
51                 public static void ReleaseConnection( MySqlInternalConnection connection )
52                 {
53                         lock (pools.SyncRoot) 
54                         {
55                                 string key = connection.Settings.ConnectString;
56                                 MySqlPool pool = (MySqlPool)pools[ key ];
57                                 if (pool == null)
58                                         throw new MySqlException("Pooling exception: Unable to find original pool for connection");
59                                 pool.ReleaseConnection(connection);
60                         }
61                 }
62         }
63 }