void startPool(String site, AbstractPoolSource source) { // System.Console.WriteLine("Instantiating connection pool"); LogUtils.getInstance().Log("Instantiating connection pool for site " + site); if (_pools[site] == null) { lock (_instantiationLocker) // need to lock here so multiple threads don't try creating the same pool { if (_pools[site] == null) // and then check again after lock is entered { _pools[site] = (ConnectionPool)ConnectionPoolFactory.getResourcePool(source); } } } }
/// <summary> /// Get and start the resource pool /// </summary> /// <param name="source">The source for the resource pool</param> /// <returns>AbstractResourcePool</returns> public static AbstractResourcePool getResourcePool(AbstractPoolSource source) { if (source is connection.ConnectionPoolSource) { connection.ConnectionPoolFactory connectionPoolFactory = new connection.ConnectionPoolFactory(); return connectionPoolFactory.getPool(source); } else if (source is connection.ConnectionPoolsSource) { connection.ConnectionPoolFactory connectionPoolFactory = new connection.ConnectionPoolFactory(); return connectionPoolFactory.getPools(source); // notice the 's' - getting a ConnectionPools object } else { throw new NotImplementedException(); } }
/// <summary> /// Get and start the resource pool /// </summary> /// <param name="source">The source for the resource pool</param> /// <returns>AbstractResourcePool</returns> public static AbstractResourcePool getResourcePool(AbstractPoolSource source) { if (source is connection.ConnectionPoolSource) { connection.ConnectionPoolFactory connectionPoolFactory = new connection.ConnectionPoolFactory(); return(connectionPoolFactory.getPool(source)); } else if (source is connection.ConnectionPoolsSource) { connection.ConnectionPoolFactory connectionPoolFactory = new connection.ConnectionPoolFactory(); return(connectionPoolFactory.getPools(source)); // notice the 's' - getting a ConnectionPools object } else { throw new NotImplementedException(); } }
/// <summary> /// Start the connection pool. This method is rather slow when run in production (130+ sites). A connection pool is started for /// each of the sources. A loop is used to start each pool. Each new pool start waits for the previous pool to come to a reasonable /// state before starting. If the previous pool hasn't finished starting after 60 seconds, the current pool will go ahead /// and start anyways. /// </summary> /// <param name="source">The source for all the connection pool configuration information</param> void startUp(ConnectionPoolsSource source) { // first things first - initialize the pool collection based off the connection sources! _pools = new Dictionary <string, ConnectionPool>(); foreach (string siteId in source.CxnSources.Keys) { _pools.Add(siteId, null); } if (source.LoadStrategy == LoadingStrategy.Lazy) { return; // lazy loading? we're all done with startup! } // the rest of this code starts the pool for each of the connection sources string[] allKeys = new string[source.CxnSources.Count]; source.CxnSources.Keys.CopyTo(allKeys, 0); IList <ConnectionPool> allPools = new List <ConnectionPool>(allKeys.Length); for (int i = 0; i < allKeys.Length; i++) { DateTime lastPoolStart = DateTime.Now; // starting 130+ connection pool threads takes a lot of system resources - we should try and let the // previous pool come up or at least give it a reasonable time to start before moving to the next connection pool if (i > 0) { while (lastPoolStart.Subtract(DateTime.Now).TotalSeconds < 60 && allPools[i - 1].TotalResources < allPools[i - 1].PoolSource.MinPoolSize) { System.Threading.Thread.Sleep(500); } } // go ahead and start the pool now ConnectionPool cxnPool = (ConnectionPool)ConnectionPoolFactory.getResourcePool(source.CxnSources[allKeys[i]]); allPools.Add(cxnPool); _pools[allKeys[i]] = cxnPool; } }
internal void run() { // never let two processes call run!!! lock (_locker) { ConnectionPoolsSource poolSource = (ConnectionPoolsSource)this.PoolSource; // just cast this once // startup _starting = true; startUp(poolSource); _starting = false; while (!Convert.ToBoolean(SHUTDOWN_FLAG)) { System.Threading.Thread.Sleep(1000); // babysit pools foreach (string siteId in poolSource.CxnSources.Keys) { if (_pools[siteId] == null) // if pool hasn't been instantiated { if (this.PoolSource.LoadStrategy == LoadingStrategy.Lazy) // and loading lazily then just continue { continue; } else if (this.PoolSource.LoadStrategy == LoadingStrategy.Eager) // and loading eagerly then instantiate pool { _pools[siteId] = (ConnectionPool)ConnectionPoolFactory.getResourcePool(poolSource.CxnSources[siteId]); } } // need to either modify isAlive to recognize idle pool or leave commented out/delete because this is re-starting the pool evert time. //else if (!_pools[siteId].IsAlive && !Convert.ToBoolean(SHUTDOWN_FLAG)) // if the pool is not started for some reason and we're not shutting down //{ // //_pools[siteId].shutdown(); // //startPool(siteId, poolSource.CxnSources[siteId]); // //_pools[siteId] = (ConnectionPool)ConnectionPoolFactory.getResourcePool(poolSource.CxnSources[siteId]); //} } } } }
/// <summary> /// Checkout a connection /// </summary> /// <param name="obj">The connection identifier (usually the site ID)</param> /// <returns>AbstractConnection</returns> public override AbstractResource checkOut(object obj) { while (_starting) // block while startup is occurring { System.Threading.Thread.Sleep(10); } if (!(obj is String)) { throw new ArgumentException("Must supply the ID of the connection pool to check out a connection"); } string site = (String)obj; // first make sure we have a dictionary key/queue for this site - if lazy loading then create a new pool for site - else exception ConnectionPoolsSource source = (ConnectionPoolsSource)this.PoolSource; if (!_pools.ContainsKey(site)) { if (!source.CxnSources.ContainsKey(site)) { throw new ArgumentException("No configuration information available for that connection pool ID ({0}) - unable to start", site); } _pools.Add(site, null); } if (_pools[site] == null) { if (this.PoolSource.LoadStrategy == LoadingStrategy.Lazy) { _pools[site] = (ConnectionPool)ConnectionPoolFactory.getResourcePool(source.CxnSources[site]); } else // treating this as an error case - if we're not lazy loading and the pool hasn't already been initialized then assume pool is being used incorrectly { throw new ConfigurationErrorsException("The pools have not been initialized properly to service your request"); } } // try and check out a connection from the pool return(_pools[site].checkOut(null)); }