Quantcast
Channel: SQL Server High Availability and Disaster Recovery forum
Viewing all articles
Browse latest Browse all 4689

Help on Jobs in ALWAYS ON AVAILABILITY GROUPS

$
0
0

I have Setup Always on Availability Groups between 2 servers( 1 active  server read/write and 1 passive not readable )and i have a Script that i wanted to put in Jobs and i want to run the jobs only on primary at any time..

Server 1 is primary---jobs check if primary , if yes execute the job

Server 2 is secondary--Jobs check if primary, if no then exit from job ....

when failover happens 

Server 1 is secondary--Jobs check if primary, if no then exit from job ....

Server 2 is primary---jobs check if primary , if yes execute the job

so i am using below scripts to test with a TESTDB database which is Read/;write on primary and not readable on secondary..

jobs runs well at Primary , but on secondary it fails ... 

USE MASTER 
GO
DECLARE @ServerName NVARCHAR(256)  = @@SERVERNAME 
DECLARE @RoleDesc NVARCHAR(60)

SELECT @RoleDesc = a.role_desc
    FROM sys.dm_hadr_availability_replica_states AS a
    JOIN sys.availability_replicas AS b
        ON b.replica_id = a.replica_id
WHERE b.replica_server_name = @ServerName

IF @RoleDesc = 'PRIMARY'



BEGIN
USE TestAG
Insert into CheckAvailabilityRoleJobs(InsertDate,ServerName,RoleDescription)
SELECT GETDATE(),@@SERVERNAME, a.role_desc
FROM      sys.dm_hadr_availability_replica_states AS a
JOIN sys.availability_replicas AS b ON b.replica_id = a.replica_id
where   b.replica_server_name=@@SERVERNAME
END


ELSE 

BEGIN 

      RETURN   
END 

I wanted to keep this job running 24X7 on both servers, but insert happens only on Primary at any time , in secondary the JOB exits with out actually inserting...

the script  is running good on PRIMARY, but when i try to run on SECONDARY it is failing ...

the script should actually exit on SECONDARY after checking Primary or not but it is trying to connect TESTAG database which is not accessible on secondary

Msg 976, Level 14, State 1, Line 16
The target database, 'TestAG', is 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.

any help?

 

Viewing all articles
Browse latest Browse all 4689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>