Skip to content

Flatten | SqlInternalConnectionTds and SqlInternalConnection - #3773

Merged
benrr101 merged 39 commits into
mainfrom
dev/russellben/flatten/sqlinternalconnection
Dec 26, 2025
Merged

benrr101 merged 39 commits into
mainfrom
dev/russellben/flatten/sqlinternalconnection

Conversation

@benrr101

Copy link
Copy Markdown
Contributor

Description

This PR is part of an ongoing (undocumented) effort to clean up, and modernize the codebase. In this installment, we have the SqlInternalConnection and SqlInternalConnectionTds class. These are a remnant of when context connections existed - two classes inherited from SqlInternalConnection: SqlInternalConnectionTds and SqlInternalConnectionSmi (I think). The SMI version was removed a while ago as part of removing functionally dead codepaths. As such, there's no need for all this inheritance. We can merge SqlInternalConnectionTds into SqlInternalConnection.

At the same time, we also move SqlInternalConnectionTds into the Connection namespace, and rename it to SqlConnectionInternal to alphabetically organize it with SqlConnection.

Comments have been added to the PR to highlight key changes in the code.

Issues

N/A

Testing

Code continues to compile, CI will validate.

@benrr101 benrr101 added this to the 7.0.0-preview3 milestone Nov 14, 2025
@benrr101 benrr101 added the Code Health 💊 Issues/PRs that are targeted to source code quality improvements. label Nov 14, 2025
Copilot AI review requested due to automatic review settings November 14, 2025 20:20
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 14, 2025
@benrr101 benrr101 changed the title Dev/russellben/flatten/sqlinternalconnection Flatten | SqlInternalConnectionTds and SqlInternalConnection Nov 14, 2025

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 connection class hierarchy by merging SqlInternalConnectionTds into SqlInternalConnection and renaming the merged class to SqlConnectionInternal. The class has been moved to the Microsoft.Data.SqlClient.Connection namespace for better organization. Additionally, a new CachedContexts class has been introduced to centralize the management of cached asynchronous call contexts.

Key changes:

  • Elimination of unnecessary inheritance between SqlInternalConnection and SqlInternalConnectionTds
  • Renaming to SqlConnectionInternal for alphabetical alignment with SqlConnection
  • Introduction of CachedContexts helper class to manage async call context caching
  • Updates to all references across the codebase to use the new type name and namespace

Reviewed Changes

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

Show a summary per file
File Description
SqlConnectionInternal.cs New merged class combining SqlInternalConnection and SqlInternalConnectionTds
CachedContexts.cs New helper class for managing cached async contexts
SqlTransaction.cs Updated type references and added namespace import
SqlInternalTransaction.cs Updated type references and added namespace import
TdsParser.cs Updated type references throughout
SqlUtil.cs Updated method signatures to use new type name
SqlException.cs Updated method signatures to use new type name
SqlDelegatedTransaction.cs Updated type references and added namespace import
SqlDataReader.cs Updated to use CachedContexts for async context management
SqlConnection.cs Updated all internal connection references
SqlCommand.cs Updated type references and added namespace import
SqlCommand.*.cs Updated async context management to use CachedContexts
SqlBulkCopy.cs Updated type references and added namespace import
SqlConnectionFactory.cs Updated instantiation to use new type name
AdapterUtil.cs Updated method signatures and added namespace import
*.csproj Updated project files to reference new file locations
Comments suppressed due to low confidence (10)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:1036

  • The Dispose() method no longer calls base.Dispose() as it did in the original SqlInternalConnection.cs. This could lead to resource leaks if the base class DbConnectionInternal has cleanup logic that needs to be executed. The base class's Dispose() method should be called to ensure proper cleanup of inherited resources.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:68
  • The GUID value has been changed from lowercase to uppercase (from 1c742caf-6680-40ea-9c26-6b6846079764 to 1C742CAF-6680-40EA-9C26-6B6846079764). While GUIDs are case-insensitive, this change appears unintentional and could lead to confusion. If this was intentional, it should be documented in the PR description.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:821
  • The property IsAzureSqlConnection was originally an internal property in SqlInternalConnection.cs, but is now marked as private in SqlConnectionInternal.cs. This could be a breaking change if other parts of the codebase outside this class relied on accessing this property. Please verify that this access modifier change is intentional and doesn't break existing functionality.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:651
  • The properties IsGlobalTransaction and IsGlobalTransactionsEnabledForServer were originally internal in SqlInternalConnection.cs, but are now marked as private in the new merged class. This appears to be an unintentional access modifier change that could break code that depends on these properties being accessible from outside the class.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:639
  • The property IsEnlistedInTransaction was originally private in SqlInternalConnection.cs, but is now marked as internal in SqlConnectionInternal.cs. While this expands access rather than restricting it, this change should be intentional and documented. If the expanded access is not needed, it should remain private to maintain encapsulation.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:724
  • The property PromotedDtcToken was originally internal with both getter and setter in SqlInternalConnection.cs, but is now internal with only a private setter. This could be a breaking change if other code sets this property from outside the class. Please verify this change is intentional.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:532
  • The property CurrentDatabase was originally internal with both getter and setter in SqlInternalConnection.cs, but now has a private setter. This could be a breaking change if other code sets this property from outside the class. Please verify this change is intentional.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs:1065
  • Variable this._activeConnection may be null at this access as suggested by this null check.
                $"Client Connection Id {_activeConnection.ClientConnectionId}, " +

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:2438

  • Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs:5805
  • Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
                    if (_connection?.InnerConnection is SqlConnectionInternal sqlInternalConnection)
                    {
                        _snapshot = sqlInternalConnection.CachedContexts.ClearDataReaderSnapshot() ?? new Snapshot();
                    }
                    else
                    {
                        _snapshot = new Snapshot();
                    }

…eInternal into ChangeDatabase, remove abstract ChangeDatabaseInternal
…SqlInternalConnection into that. Migrate usages of ExecuteReaderAsyncCallContext to the new class.
Copilot AI review requested due to automatic review settings December 2, 2025 00:20

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 22 out of 22 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (4)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:20

  • The using directive using Microsoft.Data.SqlClient.Connection; is redundant since this file is already in the Microsoft.Data.SqlClient.Connection namespace. Types within the same namespace are automatically accessible without an explicit using directive.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:67
  • Grammar issue in comment: "Rename to a match naming conventions" should be "Rename to match naming conventions"
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:2441
  • Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs:5805
  • Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
                    if (_connection?.InnerConnection is SqlConnectionInternal sqlInternalConnection)
                    {
                        _snapshot = sqlInternalConnection.CachedContexts.TakeDataReaderSnapshot() ?? new Snapshot();
                    }
                    else
                    {
                        _snapshot = new Snapshot();
                    }

mdaigle
mdaigle previously approved these changes Dec 2, 2025
@paulmedynski paulmedynski self-assigned this Dec 2, 2025
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board Dec 2, 2025
paulmedynski
paulmedynski previously approved these changes Dec 2, 2025
@benrr101
benrr101 dismissed stale reviews from paulmedynski and mdaigle via d19863d December 9, 2025 23:13
mdaigle
mdaigle previously approved these changes Dec 9, 2025
Copilot AI review requested due to automatic review settings December 11, 2025 00:02

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 23 out of 23 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (4)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:2521

  • Condition is always not null because of call to method Assert.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:2393
  • Condition is always not null because of call to method Assert.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs:2441
  • Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs:5805
  • Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
                    if (_connection?.InnerConnection is SqlConnectionInternal sqlInternalConnection)
                    {
                        _snapshot = sqlInternalConnection.CachedContexts.TakeDataReaderSnapshot() ?? new Snapshot();
                    }
                    else
                    {
                        _snapshot = new Snapshot();
                    }

@benrr101
benrr101 merged commit 3895d69 into main Dec 26, 2025
@benrr101
benrr101 deleted the dev/russellben/flatten/sqlinternalconnection branch December 26, 2025 18:54
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Code Health 💊 Issues/PRs that are targeted to source code quality improvements.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants