开发者

Can you enable Sql Cache Dependency on a database and/or table solely via Sql scripts?

开发者 https://www.devze.com 2023-04-09 08:35 出处:网络
We would like to enable Sql Cache Dependency on a database and a few tables, but we would like to avoid using aspnet_regsql.exe if possible.The database is already in our staging environment and it wi

We would like to enable Sql Cache Dependency on a database and a few tables, but we would like to avoid using aspnet_regsql.exe if possible. The database is already in our staging environment and it will be easier to just send the admins a Sql script instead of requiring batch files as well. Is it possible to enable Sql Cache Dependency entirely through Sql scripts, and if so, how?

开发者_运维知识库

Thanks!


aspnet_reqsql.exe doesn't really do anything to 'enable' SqlCacheDependency. SqlCacheDependency is based on SqlDependency which uses SqlNotificationRequest which is the .net side of a Query Notification. Query Notifications are enabled always and cannot be disabled. Query Notifications uses Service Broker to deliver the notification, which is also enabled by default, but which can be disabled in a database. The typical operation that will disable Service Broker in a database is attach or restore. To make sure you do not fall victim to this problem, all you need to do (and this is exactly what aspnet_regsql does) is to run this:

ALTER DATABASE [<dbname>] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;

Another problem that might occur is that the EXECUTE AS context required by Service Broker message delivery may be busted. For a lengthy discussion on how the EXECUTE AS can get busted, see SQL Server stops loading assembly, and the solution is trivial:

ALTER AUTHORIZATION ON DATABASE::[<dbname>] TO sa;

And finally, if you want to understand how all these work, read The Mysterious Notification.

0

精彩评论

暂无评论...
验证码 换一张
取 消