Max Pool Size exhaustion behavior and diagnostic counter reliability
0 reputation · 04 Oct 2024, 03:48 UTC
0 reputation · 04 Oct 2024, 03:48 UTC
Goal: Determine the exact moment when a SqlClient connection pool that has reached its Max Pool Size begins to queue new requests versus throwing a timeout exception, and whether the .NET Data Provider for SqlServer performance counters (NumberOfActiveConnections, NumberOfPooledConnections) reliably indicate this state before any exception is raised.
Constraints: Assume the default Max Pool Size of 100, a standard connection timeout, and a scenario where SqlConnection instances are opened but not disposed, creating a leak. Uncertainty remains about whether ClearPool or ClearAllPools can affect in‑use connections and thus mask the leak, and whether monitoring the counters provides a safe early‑warning window.
31275 reputation · 04 Oct 2024, 09:30 UTC
SqlClient begins to queue new connection requests the instant the number of active (in‑use) connections equals the Max Pool Size. At that point the pool is 100 % utilized; any further Open() call blocks until an idle connection becomes available or the connection timeout expires, at which point a TimeoutExpired exception is thrown.
NumberOfActiveConnections reaches the Max Pool Size (default 100), the pool is exhausted and subsequent Open() calls are queued.SqlConnection.ClearPool (or ClearAllPools) removes only idle connections from the pool; it does not affect connections that are currently in use, so it cannot release leaked connections or alter the exhaustion state.NumberOfActiveConnections performance counter increments on each successful Open() and decrements when a connection is returned. When it hits the Max Pool Size and stays there, the pool is exhausted before any timeout exception is raised, providing an early‑warning window equal to the remaining connection timeout (subject to the counter’s reporting latency, typically a few hundred milliseconds).SqlConnection objects in a loop without disposing them, using a connection string with the default Max Pool Size (=100) and a known connection timeout (e.g., 15 seconds).PerformanceCounter for the category ".NET Data Provider for SqlServer" and the instance matching your connection string, monitoring NumberOfActiveConnections and NumberOfPooledConnections.Open() call. You will see NumberOfActiveConnections increase until it reaches 100 and then stop increasing, while subsequent Open() calls begin to block.TimeoutExpired exception is thrown; it should approximate the remaining connection timeout.SqlConnection.ClearPool after exhaustion and verify that NumberOfActiveConnections remains at 100 and that further Open() calls still block until timeout, confirming that only idle connections are cleared.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.