2 using ByteFX.Data.Common;
3 using System.Collections;
5 namespace ByteFX.Data.MySqlClient
8 /// Summary description for MySqlPool.
10 internal sealed class MySqlPool
12 private ArrayList inUsePool;
13 private ArrayList idlePool;
17 public MySqlPool(int minSize, int maxSize)
19 this.minSize = minSize;
20 this.maxSize = maxSize;
21 inUsePool =new ArrayList(minSize);
22 idlePool = new ArrayList(minSize);
25 private MySqlInternalConnection GetPooledConnection()
27 lock (idlePool.SyncRoot)
29 foreach (MySqlInternalConnection conn in idlePool)
35 inUsePool.Add( conn );
37 idlePool.Remove( conn );
43 idlePool.Remove(conn);
50 private MySqlInternalConnection CreateNewPooledConnection( MySqlConnectionString settings )
52 MySqlInternalConnection conn = new MySqlInternalConnection( settings );
57 public void ReleaseConnection( MySqlInternalConnection connection )
59 lock (inUsePool.SyncRoot)
60 lock (idlePool.SyncRoot)
62 inUsePool.Remove( connection );
63 if (connection.Settings.ConnectionLifetime != 0 && connection.IsTooOld())
66 idlePool.Add( connection );
70 public MySqlInternalConnection GetConnection(MySqlConnectionString settings)
72 MySqlInternalConnection conn;
74 DateTime start = DateTime.Now;
78 conn = GetPooledConnection();
80 conn = CreateNewPooledConnection( settings );
81 ts = DateTime.Now.Subtract( start );
82 } while (conn == null && ts.Seconds < settings.ConnectTimeout );
85 // if pool size is at maximum, then we must have reached our timeout so we simply
86 // throw our exception
88 throw new MySqlException("error connecting: Timeout expired. The timeout period elapsed " +
89 "prior to obtaining a connection from the pool. This may have occurred because all " +
90 "pooled connections were in use and max pool size was reached.");
93 //System.Diagnostics.Debug.WriteLine("Creating a new driver");
94 /* Driver driver = new Driver();
97 driver.Connection = conn;
98 driver.Open( conn.DataSource, conn.Port, conn.User, conn.Password, conn.UseCompression );
100 driver.SendCommand( DBCmd.INIT_DB, connString["database"] );
104 throw new MySqlException("Database initialization failed with message: " + ex.Message);