/// <devdoc>
/// <para>
/// Attempts to return a PooledStream to the pool. If canReuse is false, then the
/// connection will be destroyed even if it is marked as reusable and a new conneciton will
/// be created. If it is true, then the connection will still be checked to ensure that
/// it can be pooled and will be cleaned up if it can not for another reason.
/// </para>
/// </devdoc>
internal void PutConnection(PooledStream pooledStream, object owningObject, int creationTimeout, bool canReuse)
{
GlobalLog.Print("ConnectionPool#" + ValidationHelper.HashString(this) + "::PutConnection");
if (pooledStream == null)
{
throw new ArgumentNullException("pooledStream");
}
pooledStream.PrePush(owningObject);
if (m_State != State.ShuttingDown)
{
pooledStream.Deactivate();
// cancel our error status, if we have no new requests waiting anymore
if (m_WaitCount == 0)
{
CancelErrorCallback();
}
if (canReuse && pooledStream.CanBePooled)
{
PutNew(pooledStream);
}
else
{
try {
Destroy(pooledStream);
} finally { // Make sure to release the mutex even under error conditions.
// Make sure we recreate a new pooled stream, if there are requests for a stream
// at this point
if (m_WaitCount > 0)
{
if (!CreationMutex.WaitOne(creationTimeout, false))
{
Abort();
}
else
{
try {
pooledStream = UserCreateRequest();
if (null != pooledStream)
{
PutNew(pooledStream);
}
} finally {
CreationMutex.ReleaseMutex();
}
}
}
}
}
}
else
{
// If we're shutting down, we destroy the object.
Destroy(pooledStream);
}
}