Refactor ForceNewConnection#4415
Conversation
There was a problem hiding this comment.
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 SqlConnection → DbConnectionInternal → SqlConnectionFactory). It also removes the unused PrepareForReplaceConnection() hook from DbConnectionInternal.
Changes:
- Remove
SqlConnection.ForceNewConnectionand propagate aforceNewConnectionparameter through open/reconnect code paths. - Update connection factory + provider-base open logic to use the explicit
forceNewConnectionparameter when selectingReplaceConnectionvs pooledTryGetConnection. - 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. |
benrr101
left a comment
There was a problem hiding this comment.
Love it! One request to improve readability, buuuuuuuuuuuuuuut I won't throw a fit over it.
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
ForceNewConnectionproperty set onSqlConnection. 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.