Sql-server – dropping SYNONYM is blocking in sql server

blockinglockingsql serversql-server-2012t-sql

I have synonyms which points to a TestDB1, by the next refresh synonyms point to TestDB2. while re-pointing from TestDB1 to TestDB2 it will drop all synonyms and re-creates them for TestDB2.

Now my problems is: while users querying from synonyms which points to TestDB1 is still running it won't let me drop synonyms. It is blocking the process synonyms re-point to TestDB2.

It only let me re-point after queries complete. Is there any other process to overcome this problem. I want to re-point synonyms while users querying?

Note: Both databases contains same tables but data will be change

Best Answer

No, dropping a synonym requires a Sch-M lock, and also IX locks on sysschobjs/sysobjvalues, at least if it is on the same server (I haven't tested what happens when the synonym uses a 4-part name).

What exactly would it mean if you could change a synonym in the middle of a query or transaction? If you have a query plan operator or a cursor which needs to go back to the table, or you have multiple steps in a transaction that reference the table, it would violate all kinds of principles if the referenced table were different. I think it is much better that you can't change it until a point where nobody is referencing it.