using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Blizzard { /// /// Represents a collection of re-usable MySQLConnection instances. /// sealed class MySQLConnectionPool { /// /// Pool of MySQLConnection instances. /// private Stack pool; /// /// Initializes the object pool to the specified size. /// /// Maximum number of MySQLConnection objects the pool can hold. public MySQLConnectionPool(Int32 capacity) { this.pool = new Stack(capacity); } /// /// Removes a MySQLConnection instance from the pool. /// /// MySQLConnection removed from the pool. public MySQLConnection Pop() { lock (this.pool) { if (this.pool.Count > 0) { return this.pool.Pop(); } else { return null; } } } /// /// Add a MySQLConnection instance to the pool. /// /// MySQLConnection instance to add to the pool. public void Push(MySQLConnection item) { lock (this.pool) { if (item == null) { throw new ArgumentNullException("Items added to a MySQLConnectionPool cannot be null"); } else { this.pool.Push(item); } } } /// /// Removes all instances of MySQLConnection from the pool. /// public void Clear() { lock (this.pool) { this.pool.Clear(); } } } }