/// <summary>
/// It is assumed that this method is only called from inside an active lock.
/// </summary>
private Driver GetPooledConnection()
{
Driver driver = null;
// if we don't have an idle connection but we have room for a new
// one, then create it here.
lock ((idlePool as ICollection).SyncRoot)
{
if (HasIdleConnections)
{
driver = idlePool.Dequeue();
}
}
// Obey the connection timeout
if (driver != null)
{
try
{
driver.ResetTimeout((int)Settings.ConnectionTimeout * 1000);
}
catch (Exception)
{
driver.Close();
driver = null;
}
}
if (driver != null)
{
// first check to see that the server is still alive
if (!driver.Ping())
{
driver.Close();
driver = null;
}
else if (settings.ConnectionReset)
{
// if the user asks us to ping/reset pooled connections
// do so now
driver.Reset();
}
}
if (driver == null)
{
driver = CreateNewPooledConnection();
}
Debug.Assert(driver != null);
lock ((inUsePool as ICollection).SyncRoot)
{
inUsePool.Add(driver);
int currentlyInUse = inUsePool.Count;
if (currentlyInUse > maxUsedConnections)
{
maxUsedConnections = currentlyInUse;
}
}
return(driver);
}