Skip to content

Implement ChannelDbConnectionPool.Clear#4194

Merged
mdaigle merged 11 commits intomainfrom
dev/mdaigle/pool-clear
Apr 23, 2026
Merged

Implement ChannelDbConnectionPool.Clear#4194
mdaigle merged 11 commits intomainfrom
dev/mdaigle/pool-clear

Conversation

@mdaigle
Copy link
Copy Markdown
Contributor

@mdaigle mdaigle commented Apr 14, 2026

Description

Adds an implementation for IDbConnectionPool.Clear() to ChannelDbConnectionPool.

Testing

Adds new unit tests and hooks into existing manual tests.

Copilot AI review requested due to automatic review settings April 14, 2026 19:59
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Apr 14, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds “pool clear” behavior for the new channel-based connection pool implementation (pool v2) so that SqlConnection.ClearPool() / SqlConnection.ClearAllPools() can invalidate pooled connections using a generation-counter approach.

Changes:

  • Implemented ChannelDbConnectionPool.Clear() and added per-connection ClearGeneration stamping/validation to lazily invalidate busy connections.
  • Enabled pool v2 creation via DbConnectionPoolGroup and updated pooled-connection creation to use pool.PoolGroup metadata.
  • Added/updated unit + manual tests and introduced a draft spec describing expected clear semantics.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolTest.cs Replaces “Clear not implemented” test with a suite validating clear + generation behavior.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionPoolTest/ConnectionPoolTest.cs Extends ClearAllPoolsTest to run under both pool v1 and v2 via an AppContext switch helper.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Common/SystemDataInternals/ConnectionPoolHelper.cs Updates reflection helpers to recognize channel pool and use interface-based Count.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs Simplifies pooled connection creation to use pool.PoolGroup options/key.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs Updates call to CreatePooledConnection to match new signature.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/DbConnectionPoolGroup.cs Switches pool v2 branch from throwing to instantiating ChannelDbConnectionPool.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/ChannelDbConnectionPool.cs Implements Clear() and enforces generation checks in liveliness evaluation + new connection stamping.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs Adds ClearGeneration property used by channel pool to detect stale connections.
specs/001-pool-clear/spec.md Adds a draft feature spec documenting scenarios/requirements for pool clear.
specs/001-pool-clear/checklists/requirements.md Adds a requirements checklist for the spec.

@mdaigle mdaigle added this to the 7.1.0-preview1 milestone Apr 14, 2026
@mdaigle mdaigle added the Area\Connection Pooling Use this label to tag issues that apply to problems with connection pool. label Apr 14, 2026
@mdaigle mdaigle marked this pull request as ready for review April 14, 2026 20:07
@mdaigle mdaigle requested a review from a team as a code owner April 14, 2026 20:07
@mdaigle mdaigle marked this pull request as draft April 14, 2026 21:27
Copilot AI review requested due to automatic review settings April 14, 2026 21:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements pool clearing for the channel-based connection pool (pool v2) so that SqlConnection.ClearPool() / SqlConnection.ClearAllPools() can invalidate pooled connections when UseConnectionPoolV2 is enabled.

Changes:

  • Added a generation-based Clear() implementation to ChannelDbConnectionPool, plus idle-connection tracking via a new IdleConnectionChannel.
  • Extended IDbConnectionPool with IdleCount and implemented it in WaitHandleDbConnectionPool and ChannelDbConnectionPool.
  • Updated/add tests and manual test plumbing to validate clear semantics and to support running ClearAllPools tests against both pool implementations.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/TransactedConnectionPoolTest.cs Updates test mock to satisfy the extended IDbConnectionPool interface (IdleCount).
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/IdleChannelTest.cs Adds unit tests validating idle-channel counting and read/write behavior.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolTest.cs Updates expectations (e.g., ErrorOccurred) and adds clear-focused unit tests.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionPoolTest/ConnectionPoolTest.cs Runs ClearAllPools coverage for both pool v1 and pool v2 via an AppContext switch helper.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Common/SystemDataInternals/ConnectionPoolHelper.cs Updates reflection helpers to use interface properties (Count/IdleCount) and handle channel pool cleanup differences.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs Simplifies pooled-connection creation to use pool.PoolGroup instead of passing redundant options/key args.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs Implements IdleCount and updates internal usage to use the new property.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/IdleChannel.cs Introduces IdleConnectionChannel to wrap an unbounded channel and track non-null idle count.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/IDbConnectionPool.cs Adds IdleCount to the pool interface.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/DbConnectionPoolGroup.cs Enables constructing ChannelDbConnectionPool when UseConnectionPoolV2 is enabled.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/ChannelDbConnectionPool.cs Implements Clear() with a generation counter; adds idle tracking and clears stale connections on return/retrieval.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs Adds ClearGeneration tracking on internal connections for pool v2 stale detection.
specs/001-pool-clear/spec.md Adds a feature spec for pool clear behavior and acceptance scenarios.
specs/001-pool-clear/checklists/requirements.md Adds a spec quality checklist for the new feature spec.

@mdaigle mdaigle marked this pull request as ready for review April 14, 2026 22:17
@mdaigle mdaigle changed the title Dev/mdaigle/pool clear Implement ChannelDbConnectionPool.Clear Apr 14, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 14, 2026

Codecov Report

❌ Patch coverage is 94.11765% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.41%. Comparing base (8cd5dda) to head (ccdf316).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...qlClient/ConnectionPool/ChannelDbConnectionPool.cs 90.90% 4 Missing ⚠️
.../SqlClient/ConnectionPool/IdleConnectionChannel.cs 97.14% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4194      +/-   ##
==========================================
- Coverage   65.99%   64.41%   -1.58%     
==========================================
  Files         276      272       -4     
  Lines       42951    65812   +22861     
==========================================
+ Hits        28344    42395   +14051     
- Misses      14607    23417    +8810     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.41% <94.11%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread specs/001-pool-clear/spec.md
Comment thread specs/001-pool-clear/spec.md
Comment thread specs/001-pool-clear/spec.md
Comment thread specs/001-pool-clear/checklists/requirements.md Outdated
@paulmedynski paulmedynski removed this from the 7.1.0-preview1 milestone Apr 21, 2026
@paulmedynski paulmedynski added this to the 7.1.0-preview2 milestone Apr 21, 2026
@paulmedynski paulmedynski self-assigned this Apr 22, 2026
@paulmedynski paulmedynski moved this from To triage to In review in SqlClient Board Apr 22, 2026
Copy link
Copy Markdown
Contributor

@paulmedynski paulmedynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, with mostly questions to be answered.

Comment thread specs/001-pool-clear/spec.md Outdated
Comment thread specs/001-pool-clear/spec.md
Comment thread specs/001-pool-clear/spec.md Outdated
@github-project-automation github-project-automation Bot moved this from In review to In progress in SqlClient Board Apr 22, 2026
mdaigle and others added 4 commits April 22, 2026 15:06
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Copilot AI review requested due to automatic review settings April 22, 2026 23:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements IDbConnectionPool.Clear() for the channel-based pooling implementation (pool v2) using a generation-counter approach, and updates tests/docs to validate pool clearing behavior.

Changes:

  • Add Clear() implementation to ChannelDbConnectionPool and introduce IdleConnectionChannel to track idle-connection counts for an unbounded channel.
  • Extend IDbConnectionPool with IdleCount and update v1 pool + test infrastructure accordingly.
  • Add/adjust unit tests and wire ClearAllPools manual test to run under both pool versions; update docs and add a feature spec.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/ChannelDbConnectionPool.cs Implements Clear() via generation counter; integrates IdleConnectionChannel; adjusts pool properties/behavior.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/IdleConnectionChannel.cs New helper wrapping Channel<T> with an atomic non-null idle count.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/IDbConnectionPool.cs Adds IdleCount and expands Clear() remarks.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs Implements IdleCount; uses new SqlConnectionFactory.CreatePooledConnection signature.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/DbConnectionPoolGroup.cs Enables creating ChannelDbConnectionPool when UseConnectionPoolV2 is set.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs Simplifies CreatePooledConnection to use pool group state from the pool instance.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs Adds ClearGeneration property used by pool v2 to detect stale connections after clears.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolTest.cs Updates tests for ErrorOccurred and adds coverage for pool v2 Clear() behavior.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/IdleConnectionChannelTest.cs New unit tests validating IdleConnectionChannel count semantics (sync/async/concurrent).
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/TransactedConnectionPoolTest.cs Updates IDbConnectionPool mock to include IdleCount.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionPoolTest/ConnectionPoolTest.cs Runs ClearAllPoolsTest under both pool v1 and v2 via LocalAppContextSwitchesHelper.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Common/SystemDataInternals/ConnectionPoolHelper.cs Switches “free connection count” reflection to use IDbConnectionPool.IdleCount and handles v2 cleanup differences.
doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml Improves ClearAllPools/ClearPool remarks and adds a caution note.
specs/001-pool-clear/spec.md Adds a draft feature spec describing pool-clear behavior and acceptance scenarios.

Copy link
Copy Markdown
Contributor

@benrr101 benrr101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple nits, just one question

@mdaigle mdaigle merged commit 74a8b0f into main Apr 23, 2026
303 checks passed
@mdaigle mdaigle deleted the dev/mdaigle/pool-clear branch April 23, 2026 20:49
@github-project-automation github-project-automation Bot moved this from In progress to Done in SqlClient Board Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area\Connection Pooling Use this label to tag issues that apply to problems with connection pool.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Feature | Redesign the SqlClient Connection Pool to Improve Performance and Async Support

6 participants