Skip to content

Commit 1b07f8b

Browse files
authored
Avoid some allocations when opening a connection (#3364)
1 parent 0c5155d commit 1b07f8b

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -762,29 +762,53 @@ internal static Version GetAssemblyVersion()
762762
private const string AZURE_SQL_CHINA = ".database.chinacloudapi.cn";
763763
private const string AZURE_SQL_FABRIC = ".database.fabric.microsoft.com";
764764

765-
internal static bool IsAzureSynapseOnDemandEndpoint(string dataSource)
766-
{
767-
return IsEndpoint(dataSource, ONDEMAND_PREFIX)
768-
|| dataSource.Contains(AZURE_SYNAPSE)
769-
|| dataSource.Contains(FABRIC_DATAWAREHOUSE)
770-
|| dataSource.Contains(PBI_DATAWAREHOUSE)
771-
|| dataSource.Contains(PBI_DATAWAREHOUSE2)
772-
|| dataSource.Contains(PBI_DATAWAREHOUSE3);
773-
}
774-
765+
/// <summary>
766+
/// Represents a collection of Azure SQL Server endpoint URLs for various regions and environments.
767+
/// </summary>
768+
/// <remarks>This array includes endpoint URLs for Azure SQL in global, Germany, US Government,
769+
/// China, and Fabric environments. These endpoints are used to identify and interact with Azure SQL services
770+
/// in their respective regions or environments.</remarks>
775771
internal static readonly string[] s_azureSqlServerEndpoints = { AZURE_SQL,
776772
AZURE_SQL_GERMANY,
777773
AZURE_SQL_USGOV,
778774
AZURE_SQL_CHINA,
779775
AZURE_SQL_FABRIC };
776+
777+
/// <summary>
778+
/// Contains endpoint strings for Azure SQL Server on-demand services.
779+
/// Each entry is a combination of the ONDEMAND_PREFIX and a specific Azure SQL endpoint string.
780+
/// Example format: "ondemand.database.windows.net".
781+
/// </summary>
782+
internal static readonly string[] s_azureSqlServerOnDemandEndpoints = { ONDEMAND_PREFIX + AZURE_SQL,
783+
ONDEMAND_PREFIX + AZURE_SQL_GERMANY,
784+
ONDEMAND_PREFIX + AZURE_SQL_USGOV,
785+
ONDEMAND_PREFIX + AZURE_SQL_CHINA,
786+
ONDEMAND_PREFIX + AZURE_SQL_FABRIC };
787+
/// <summary>
788+
/// Represents a collection of endpoint identifiers for Azure Synapse and related services.
789+
/// </summary>
790+
/// <remarks>This array contains predefined endpoint strings used to identify Azure Synapse and
791+
/// associated services, such as Fabric Data Warehouse and Power BI Data Warehouse.</remarks>
792+
internal static readonly string[] s_azureSynapseEndpoints = { FABRIC_DATAWAREHOUSE,
793+
PBI_DATAWAREHOUSE,
794+
PBI_DATAWAREHOUSE2,
795+
PBI_DATAWAREHOUSE3 };
780796

797+
internal static readonly string[] s_azureSynapseOnDemandEndpoints = [.. s_azureSqlServerOnDemandEndpoints, .. s_azureSynapseEndpoints];
798+
799+
internal static bool IsAzureSynapseOnDemandEndpoint(string dataSource)
800+
{
801+
return IsEndpoint(dataSource, s_azureSynapseOnDemandEndpoints)
802+
|| dataSource.IndexOf(AZURE_SYNAPSE, StringComparison.OrdinalIgnoreCase) >= 0;
803+
}
804+
781805
internal static bool IsAzureSqlServerEndpoint(string dataSource)
782806
{
783-
return IsEndpoint(dataSource, null);
807+
return IsEndpoint(dataSource, s_azureSqlServerEndpoints);
784808
}
785809

786810
// This method assumes dataSource parameter is in TCP connection string format.
787-
private static bool IsEndpoint(string dataSource, string prefix)
811+
private static bool IsEndpoint(string dataSource, string[] endpoints)
788812
{
789813
int length = dataSource.Length;
790814
// remove server port
@@ -805,8 +829,6 @@ private static bool IsEndpoint(string dataSource, string prefix)
805829
foundIndex = -1;
806830
}
807831

808-
809-
810832
if (foundIndex > 0)
811833
{
812834
length = foundIndex;
@@ -819,10 +841,9 @@ private static bool IsEndpoint(string dataSource, string prefix)
819841
}
820842

821843
// check if servername ends with any endpoints
822-
for (int index = 0; index < s_azureSqlServerEndpoints.Length; index++)
844+
foreach (var endpoint in endpoints)
823845
{
824-
string endpoint = string.IsNullOrEmpty(prefix) ? s_azureSqlServerEndpoints[index] : prefix + s_azureSqlServerEndpoints[index];
825-
if (length > endpoint.Length)
846+
if (length >= endpoint.Length)
826847
{
827848
if (string.Compare(dataSource, length - endpoint.Length, endpoint, 0, endpoint.Length, StringComparison.OrdinalIgnoreCase) == 0)
828849
{

src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ public void ConnectionString_WithOnlyComma()
11001100
[InlineData("myserver.database.windows.net")]
11011101
[InlineData("myserver.database.cloudapi.de")]
11021102
[InlineData("myserver.database.usgovcloudapi.net")]
1103+
[InlineData("myserver.DATABASE.usgovcloudapi.net")]
11031104
[InlineData("myserver.database.chinacloudapi.cn")]
11041105
[InlineData("myserver.database.fabric.microsoft.com")]
11051106
public void ConnectionRetryForAzureDbEndpoints(string serverName)
@@ -1112,8 +1113,10 @@ public void ConnectionRetryForAzureDbEndpoints(string serverName)
11121113

11131114
[Theory]
11141115
[InlineData("myserver-ondemand.sql.azuresynapse.net")]
1116+
[InlineData("myserver-ondemand.SQL.azuresynapse.net")]
11151117
[InlineData("someserver-ondemand.database.windows.net")]
11161118
[InlineData("datawarehouse.fabric.microsoft.com")]
1119+
[InlineData("datawarehouse.FABRIC.microsoft.com")]
11171120
[InlineData("datawarehouse.pbidedicated.microsoft.com")]
11181121
[InlineData("someserver.pbidedicated.microsoft.com")]
11191122
[InlineData("someserver.pbidedicated.windows.net")]

0 commit comments

Comments
 (0)