Operation cannot be performed on database because it is involved in a database mirroring session or an availability group (Azure)

ASH picture ASH · Dec 22, 2016 · Viewed 14.7k times · Source

I am experiencing an intermittent issue with SQL Azure. I am copying a readonly SQL Azure database (V12) from one elastic pool into another. Once the database is copied, I set it to read/write using:

$"ALTER DATABASE [{databaseName}] SET READ_WRITE"

This sometimes fails with the error:

The operation cannot be performed on database *** because it is involved in a database mirroring session or an availability group

I have tried implementing retry logic with backoff but that doesn't work. I have failed to find any documentation relating to this error or to database mirroring in Azure.

Any advice would be greatly appreciated!

Answer

ASH picture ASH · Feb 3, 2017

I've managed to rectify this issue and it was indeed related to geo-replication. The database I was copying originally had a geo replica but I was dropping the secondary database (using DROP DATABASE) prior to copying the primary. The fix was to first drop the replication link between the primary and secondary before dropping the secondary database (using ALTER DATABASE [dbName] REMOVE SECONDARY ON SERVER [serverName]).