Question

Working on someone else's code where they've instituted a SQL Dependency event (SQL 2008). Not something I'm terribly familiar with so was just doing some reading and some testing.

The event is called CustomerServicesTable_OnChange and so is presumably meant to fire whenever the customerServices table is changed. However when the DB is changed through code (we're using Entity Framework) the event is not firing.

Out of curiosity I fired up SQL Server Manager and tried running queries against the table directly to see what would happen. Running UPDATE, INSERT or ALTER queries against the table didn't cause the event to fire.

To check that SQL dependency had been enabled I then ran the following commands against the database, supposedly the first step in enabling the service:

ALTER DATABASE customers SET SINGLE_USER with rollback immediate
alter database customers set enable_broker
ALTER DATABASE customers SET MULTI_USER

To my surprise running these commands caused the event to fire!

So it looks as though the dependency is working against either the database as a whole, or table(s) in the DB other than CustomerServices. Can anyone suggest a way forward to help me determine exactly what the problem is?

EDIT: I have discovered that it's not the ALTER DATABASE commands which are causing the event to fire, but the fact that SET SINGLE_USER causes an error. Doesn't help me with the problem much though.

Cheers, Matt

Was it helpful?

Solution 2

Finally got this one worked out:

1) The database owner needs to be set to a SQL Server account instead of a Windows Admin account.

2) The watch service (i.e. the SQL command issued to start the notification service) needs to be set up on the same user account as will be used to make the database updates you're watching for.

I did a bunch of permissions work, but I think those are the two key issues to making it work.

OTHER TIPS

Read this first to understand how SqlDependency works: The Mysterious Notification.

Possibly the notifications are piling up in sys.transmission_queue and the transmission_status would indicate why this happens.

A shot in the dark: the dbo is mapped to invalid SID:

ALTER AUTHORIZATION ON DATABASE::customers to sa;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top