Skip to content

Refactor ForceNewConnection#4415

Merged
mdaigle merged 7 commits into
mainfrom
dev/mdaigle/replace-conn
Jul 2, 2026
Merged

Refactor ForceNewConnection#4415
mdaigle merged 7 commits into
mainfrom
dev/mdaigle/replace-conn

Conversation

@mdaigle

@mdaigle mdaigle commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

This PR is a refactor in service of #AB44829

Idle connection resiliency is a feature that replaces broken connections in-line before running a command. It utilizes a "replace" flow that follows the normal "open" flow but with a ForceNewConnection property set on SqlConnection. At certain critical points, we branch on this property to "create new" or "replace". These are really the same thing, but are exposed in a mutually exclusive way on the DbConnectionInternal subtypes. There's always one that works and the other throws NotImplementedException or InvalidOperationException.

It's very hard to understand where the property is set and when it might change. This PR removes the property and passes the desired behavior as a boolean argument.

Copilot AI review requested due to automatic review settings June 29, 2026 22:39
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jun 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the internal “replace connection” flow by removing the mutable SqlConnection.ForceNewConnection flag and instead threading an explicit forceNewConnection boolean through the open/replace pipeline (from SqlConnectionDbConnectionInternalSqlConnectionFactory). It also removes the unused PrepareForReplaceConnection() hook from DbConnectionInternal.

Changes:

  • Remove SqlConnection.ForceNewConnection and propagate a forceNewConnection parameter through open/reconnect code paths.
  • Update connection factory + provider-base open logic to use the explicit forceNewConnection parameter when selecting ReplaceConnection vs pooled TryGetConnection.
  • Remove DbConnectionInternal.PrepareForReplaceConnection() and corresponding call sites.

Reviewed changes

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

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionStateTransitionTests.cs Updates unit tests to call TryOpenInner(..., forceNewConnection) directly.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs Adds forceNewConnection parameter and uses it to choose replace-vs-get logic.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Removes ForceNewConnection; uses explicit forceNewConnection in open/reconnect paths and in async retry state.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs Removes call to the deleted PrepareForReplaceConnection() hook.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs Ensures replace-connection path calls TryOpenConnectionInternal(..., forceNewConnection: true, ...).
src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs Adds forceNewConnection argument to the internal open pipeline and passes it into the factory.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionClosed.cs Threads forceNewConnection: false for initial open from the closed state.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
Copilot AI review requested due to automatic review settings June 29, 2026 23:08
@mdaigle mdaigle added this to the 7.1.0-preview2 milestone Jun 29, 2026
@mdaigle mdaigle marked this pull request as ready for review June 29, 2026 23:08
@mdaigle mdaigle requested a review from a team as a code owner June 29, 2026 23:08
@mdaigle mdaigle changed the title Dev/mdaigle/replace conn Refactor ForceNewConnection Jun 29, 2026
@mdaigle mdaigle assigned benrr101 and unassigned cheenamalhotra Jun 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs Outdated
@github-project-automation github-project-automation Bot moved this from To triage to Waiting for customer in SqlClient Board Jun 30, 2026
@paulmedynski paulmedynski moved this from Waiting for customer to In review in SqlClient Board Jun 30, 2026
@mdaigle mdaigle requested a review from paulmedynski July 1, 2026 18:34
@mdaigle mdaigle requested a review from benrr101 July 1, 2026 18:34

@benrr101 benrr101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Love it! One request to improve readability, buuuuuuuuuuuuuuut I won't throw a fit over it.

@priyankatiwari08 priyankatiwari08 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.65517% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.01%. Comparing base (44e1b72) to head (58b03e6).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
...ient/src/Microsoft/Data/SqlClient/SqlConnection.cs 84.21% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4415      +/-   ##
==========================================
- Coverage   65.32%   64.01%   -1.32%     
==========================================
  Files         285      282       -3     
  Lines       43373    66572   +23199     
==========================================
+ Hits        28335    42616   +14281     
- Misses      15038    23956    +8918     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.01% <89.65%> (?)

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

☔ View full report in Codecov by Harness.
📢 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.

@mdaigle mdaigle enabled auto-merge (squash) July 2, 2026 16:42
@mdaigle mdaigle merged commit 1ed8646 into main Jul 2, 2026
351 of 354 checks passed
@mdaigle mdaigle deleted the dev/mdaigle/replace-conn branch July 2, 2026 17:20
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants