Recently, in an effort to install and configure BizTalk 2016 Servers with "Always On High Availability" SQL Severs, the below error occurred when we ran the BizTalk jobs on the secondary node.
[Unable to connect to SQL Server '(local)'. The step failed]
[participating in an availability group and is currently not accessible for queries. Either data movement is suspended or the availability replica is not enabled for read access. To allow read-only access to this and other databases in the availability group, enable read access to one or more secondary availability replicas in the group. For more information, see the ALTER AVAILABILITY GROUP statement in SQL Server Books Online. [SQLSTATE 42000] (Error 976). The step failed.
This is because the secondary SQL node could not read the database in primary SQL.
There are two options to resolve the issue:
1 - Enable "Readable Secondary"
Expand the Always On High Availability node, right click on Primary node and select Property. Change the value in Readable Secondary column from "No" to "Yes" and click OK
2 - Using dynamic SQL
Edit the job, select "master" under Database option. Update the command script to use dynamic SQL
DECLARE @dbname sysname = 'BizTalkMgmtDb'
IF sys.fn_hadr_is_primary_replica ( @dbname ) = 1
DECLARE @vSQL nvarchar(4000);
SET @vSQL = 'EXEC [' + @dbname + '].[dbo].[btf_PurgeExpiredMessages]'
EXEC SP_EXECUTESQL @vSQL