Given the following scenario, SQL Server will return the failover partner server name to a connection request:
SQL Server will return the failover partner server name (the SQL Server name hosting the secondary replica). The data access provider will cache the failover partner server name.
Following a failover, if an application is designed to reconnect on connection failure, it will first fail to connect to the replica now hosting a secondary replica, and then attempt to connect to the cached failover partner, the primary replica.
This behavior may be unexpected. For example, an identical SQL Agent job is created on the primary and secondary replicas of an availability group and the job is not qualifying that the replica is primary sys.fn_hadr_is_primary_replica before executing its commands. The expectation is that the job succeeds when accessing the availability database on the primary replica and fails when attempting to access the availability database on the secondary because the replica is configured for ALLOW_CONNECTIONS=NO.
Instead, following a failover, the job that is now executing at the secondary replica will not behave as expected. The next time the job executes, it will fail to connect locally and then attempt to connect using the cached failover partner name and will successfully connect to the now primary replica. This can cause unexpected behavior and results because the job at the secondary is connecting and executing successfully against the primary replica.
This behavior is by design. AlwaysOn availability groups are designed to be backward compatible with applications that expect legacy database mirroring connection behavior.
The reconnect behavior will only occur given a certain configuration, when 1) there is a single secondary replica and 2) the availability replica’s ALLOW_CONNECTIONS is set to NO or READ_ONLY. The following table reports the expected behavior depending on these variables.
If the legacy connection behavior is not desired, consider using one of the following workarounds to ensure your SQL Agent job only executes successfully when the local replica is in the primary role.
For more information on database mirroring connection behavior see:
Making the Initial Connection to a Database Mirroring Session
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.