locked
Help on Jobs in ALWAYS ON AVAILABILITY GROUPS RRS feed

  • Question

  • 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?

     
    Monday, September 28, 2015 1:20 PM

Answers

All replies

  • I made secondary server readable in Always on Properties ..it worked now
    Monday, September 28, 2015 2:13 PM
  • Have you tried running the first SELECT on your secondary manually? What does it return?
    Monday, September 28, 2015 2:59 PM
  • Hi coolguy123SQL,

    Glad to hear that the issue is resolved. You can mark your reply as answer, that way, other community members could benefit from your sharing.



    Thanks,
    Lydia Zhang


    Lydia Zhang
    TechNet Community Support


    Tuesday, September 29, 2015 1:31 AM