Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ private void CheckNotificationStateAndAutoEnlist()
}

Notification.Options = SqlDependency.GetDefaultComposedOptions(_activeConnection.DataSource,
InternalTdsConnection.ServerProvidedFailOverPartner,
InternalTdsConnection.ServerProvidedFailoverPartner,
identityUserName, _activeConnection.Database);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ internal SessionData CurrentSessionData
// FOR CONNECTION RESET MANAGEMENT
private bool _fResetConnection;
private string _originalDatabase;
private string _currentFailoverPartner; // only set by ENV change from server
private string _originalLanguage;
private string _currentLanguage;
private int _currentPacketSize;
Expand Down Expand Up @@ -704,13 +703,7 @@ internal TdsParser Parser
}
}

internal string ServerProvidedFailOverPartner
{
get
{
return _currentFailoverPartner;
}
}
internal string ServerProvidedFailoverPartner { get; set; }

internal SqlConnectionPoolGroupProviderInfo PoolGroupProviderInfo
{
Expand Down Expand Up @@ -1507,7 +1500,7 @@ private void OpenLoginEnlist(TimeoutTimer timeout,
throw SQL.ROR_FailoverNotSupportedConnString();
}

if (ServerProvidedFailOverPartner != null)
if (ServerProvidedFailoverPartner != null)
{
throw SQL.ROR_FailoverNotSupportedServer(this);
}
Expand Down Expand Up @@ -1635,7 +1628,7 @@ private void LoginNoFailover(ServerInfo serverInfo,
newSecurePassword,
attemptOneLoginTimeout);

if (connectionOptions.MultiSubnetFailover && ServerProvidedFailOverPartner != null)
if (connectionOptions.MultiSubnetFailover && ServerProvidedFailoverPartner != null)
{
// connection succeeded: trigger exception if server sends failover partner and MultiSubnetFailover is used
throw SQL.MultiSubnetFailoverWithFailoverPartner(serverProvidedFailoverPartner: true, internalConnection: this);
Expand Down Expand Up @@ -1663,7 +1656,7 @@ private void LoginNoFailover(ServerInfo serverInfo,
_currentPacketSize = ConnectionOptions.PacketSize;
_currentLanguage = _originalLanguage = ConnectionOptions.CurrentLanguage;
CurrentDatabase = _originalDatabase = ConnectionOptions.InitialCatalog;
_currentFailoverPartner = null;
ServerProvidedFailoverPartner = null;
_instanceName = string.Empty;

routingAttempts++;
Expand Down Expand Up @@ -1702,7 +1695,7 @@ private void LoginNoFailover(ServerInfo serverInfo,
// We only get here when we failed to connect, but are going to re-try

// Switch to failover logic if the server provided a partner
if (ServerProvidedFailOverPartner != null)
if (ServerProvidedFailoverPartner != null)
{
if (connectionOptions.MultiSubnetFailover)
{
Expand All @@ -1718,7 +1711,7 @@ private void LoginNoFailover(ServerInfo serverInfo,
LoginWithFailover(
true, // start by using failover partner, since we already failed to connect to the primary
serverInfo,
ServerProvidedFailOverPartner,
ServerProvidedFailoverPartner,
newPassword,
newSecurePassword,
redirectedUserInstance,
Expand All @@ -1740,8 +1733,13 @@ private void LoginNoFailover(ServerInfo serverInfo,
{
// We must wait for CompleteLogin to finish for to have the
// env change from the server to know its designated failover
// partner; save this information in _currentFailoverPartner.
PoolGroupProviderInfo.FailoverCheck(false, connectionOptions, ServerProvidedFailOverPartner);
// partner; save this information in ServerProvidedFailoverPartner.

// When ignoring server provided failover partner, we must pass in the original failover partner from the connection string.
// Otherwise the pool group's failover partner designation will be updated to point to the server provided value.
string actualFailoverPartner = LocalAppContextSwitches.IgnoreServerProvidedFailoverPartner ? "" : ServerProvidedFailoverPartner;

PoolGroupProviderInfo.FailoverCheck(false, connectionOptions, actualFailoverPartner);
}
CurrentDataSource = originalServerInfo.UserServerName;
}
Expand Down Expand Up @@ -1802,7 +1800,7 @@ TimeoutTimer timeout
ServerInfo failoverServerInfo = new ServerInfo(connectionOptions, failoverHost, connectionOptions.FailoverPartnerSPN);

ResolveExtendedServerName(primaryServerInfo, !redirectedUserInstance, connectionOptions);
if (ServerProvidedFailOverPartner == null)
if (ServerProvidedFailoverPartner == null)
{
ResolveExtendedServerName(failoverServerInfo, !redirectedUserInstance && failoverHost != primaryServerInfo.UserServerName, connectionOptions);
}
Expand Down Expand Up @@ -1861,12 +1859,21 @@ TimeoutTimer timeout
failoverDemandDone = true;
}

// Primary server may give us a different failover partner than the connection string indicates. Update it
if (ServerProvidedFailOverPartner != null && failoverServerInfo.ResolvedServerName != ServerProvidedFailOverPartner)
// Primary server may give us a different failover partner than the connection string indicates.
// Update it only if we are respecting server-provided failover partner values.
if (ServerProvidedFailoverPartner != null && failoverServerInfo.ResolvedServerName != ServerProvidedFailoverPartner)
{
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.SqlInternalConnectionTds.LoginWithFailover|ADV> {0}, new failover partner={1}", ObjectID, ServerProvidedFailOverPartner);
failoverServerInfo.SetDerivedNames(string.Empty, ServerProvidedFailOverPartner);
if (LocalAppContextSwitches.IgnoreServerProvidedFailoverPartner)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlInternalConnectionTds.LoginWithFailover|ADV> {0}, Ignoring server provided failover partner '{1}' due to IgnoreServerProvidedFailoverPartner AppContext switch.", ObjectID, ServerProvidedFailoverPartner);
}
else
{
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.SqlInternalConnectionTds.LoginWithFailover|ADV> {0}, new failover partner={1}", ObjectID, ServerProvidedFailoverPartner);
failoverServerInfo.SetDerivedNames(string.Empty, ServerProvidedFailoverPartner);
}
}

currentServerInfo = failoverServerInfo;
_timeoutErrorInternal.SetInternalSourceType(SqlConnectionInternalSourceType.Failover);
}
Expand Down Expand Up @@ -1916,7 +1923,7 @@ TimeoutTimer timeout
_currentPacketSize = connectionOptions.PacketSize;
_currentLanguage = _originalLanguage = ConnectionOptions.CurrentLanguage;
CurrentDatabase = _originalDatabase = connectionOptions.InitialCatalog;
_currentFailoverPartner = null;
ServerProvidedFailoverPartner = null;
_instanceName = string.Empty;

AttemptOneLogin(
Expand Down Expand Up @@ -1978,7 +1985,7 @@ TimeoutTimer timeout
_activeDirectoryAuthTimeoutRetryHelper.State = ActiveDirectoryAuthenticationTimeoutRetryState.HasLoggedIn;

// if connected to failover host, but said host doesn't have DbMirroring set up, throw an error
if (useFailoverHost && ServerProvidedFailOverPartner == null)
if (useFailoverHost && ServerProvidedFailoverPartner == null)
{
throw SQL.InvalidPartnerConfiguration(failoverHost, CurrentDatabase);
}
Expand All @@ -1987,8 +1994,13 @@ TimeoutTimer timeout
{
// We must wait for CompleteLogin to finish for to have the
// env change from the server to know its designated failover
// partner; save this information in _currentFailoverPartner.
PoolGroupProviderInfo.FailoverCheck(useFailoverHost, connectionOptions, ServerProvidedFailOverPartner);
// partner.

// When ignoring server provided failover partner, we must pass in the original failover partner from the connection string.
// Otherwise the pool group's failover partner designation will be updated to point to the server provided value.
string actualFailoverPartner = LocalAppContextSwitches.IgnoreServerProvidedFailoverPartner ? failoverHost : ServerProvidedFailoverPartner;

PoolGroupProviderInfo.FailoverCheck(useFailoverHost, connectionOptions, actualFailoverPartner);
}
CurrentDataSource = (useFailoverHost ? failoverHost : primaryServerInfo.UserServerName);
}
Expand Down Expand Up @@ -2218,7 +2230,8 @@ internal void OnEnvChange(SqlEnvChange rec)
{
throw SQL.ROR_FailoverNotSupportedServer(this);
}
_currentFailoverPartner = rec._newValue;

ServerProvidedFailoverPartner = rec._newValue;
break;

case TdsEnums.ENV_PROMOTETRANSACTION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ private void CheckNotificationStateAndAutoEnlist()
}

Notification.Options = SqlDependency.GetDefaultComposedOptions(_activeConnection.DataSource,
InternalTdsConnection.ServerProvidedFailOverPartner,
InternalTdsConnection.ServerProvidedFailoverPartner,
identityUserName, _activeConnection.Database);
}

Expand Down
Loading
Loading