I have the following scenario on SQL Server 2016 Standard Edition 13.0.4411.0 (SP1-CU1):
2 database servers (DBNode1 and DBNode2). I have a database in a Basic Availability Group, with DBNode1 being the Primary and DBNode2 being the Secondary. As expected, if I try connecting to the DB on DBNode2, it fails as expected with the "The target database, ..., is participating in an availability group and is currently not accessible for queries" error
I've created a SQL Server Agent Job on DBNode2 only (the Secondary) to perform a dummy UPDATE on the database every minute as a test and expected this to fail as that DB should not be accessible on that Secondary node.
UPDATE dbo.SomeTable SET Field1=GETDATE() WHERE Id = 1
The "Database" option for the job step is set to my DB. However, it succeeds and has actually performed the operation on the Primary node. (See images 1 and 2 below)
If I change the Job to target the master database, and use the following TSQL command structure, this does then error as I'd expect, being the Secondary node (See images 3 and 4 below).
UPDATE MyDB.dbo.SomeTable SET Field1=GETDATE() WHERE Id = 1
It seems like behind the scenes, despite targetting the local server, this is actually working out that the target DB is in an Availability Group and so connects to the Primary node and runs the command there. Is this normal / documented behaviour?
UPDATE:
Added screenshots to show the setup, to avoid confusion.
1 - original job setup showing the unexpected behaviour
2 - original job history output, showing the successful execution
3 - revised job setup showing the expected behaviour
4 - revised job history output, showing the failed execution as expected