I am using a table as a message queue and "signing up" for up for updates by using a SqlDependency. Everywhere I read people are saying look out for the limitations of it but not specifically saying what they are. From what I've gathered you will have problems when the table has very high update frequency, fortunately I'm only looking at 10 - 20 values per minute maximum.
What are the other limitations/impact on the SqlServer?
The most complete list I can find (from here) is as follows:
Additional reference(s):
Another big issue I have with this technology: the need for the subscriber connection to have Create Procedure permissions. The web service layer of my application at work at the moment runs as a restricted user. To get notifications setup using SQLDependency I'd have to open up that user to create procs. Sounds like a pretty good step along the path of getting owned.
In addition to this, for anyone else thinking about using SqlDependency to receive notifications about changes, I've been using this approach in production, and I'm finding problems with it. I'm looking into it to see if the problems are related to my code, but the main issues are:
If you fire multiple changes in quick succession, you don't always get the equivalent number of events coming through to the code. In my code, if 2 new records are inserted one after the other, I only get the one notification (for the last one).
There is no way to know the record that was added. So if you add a new record, and the code fires to receive the notification, there is no way in the code to know the id of that new record, so you need to query the database for it.
To overcome these limitations, you can try use the SqlTableDependency. Have a look at www.sqltabledependency.it
Note that you cannot use a nolock hint in the stored procedure or the the dependency will remain constantly invalid and therefore any cache you make on it will permanently re-query the database.
This does not appear to be mentioned in the documentation (as far as I can tell)
The following SET options are required prior to the procedure script
Others have argued that these SET options are also required but I don't think they are. It's a good idea to set then like this anyway though.
Spent a day chasing down an issue with SQL Service Broker not working, the root cause was referencing the database in the stored procedure.
For example, this
select
works fine in SQL Management Studio:However, this is rejected by SQL Service Broker because we are referencing the database in the select statement, and the callback from
SqlDependency
comes back withInvalid
inSqlNotificationEventArgs e
, see http://msdn.microsoft.com/en-us/library/ms189308.aspx.Altering the SQL passed into SqlDependency to the following statement eliminated the error:
Update
The example above is just one of many, many limitations to the SQL statement that SQL Service Broker depends on. For a complete list of limitations, see What are the limitations of SqlDependency.
The reason? The SQL statement that SQL Service Broker uses is converted, behind the scenes, into instructions to monitor the SQL Transaction Log for changes to the database. This monitoring is performed in the core of SQL Server, which makes it extremely fast when it comes to detecting changes to table(s). However, this speed comes at a cost: you can't use just any SQL statement, you must use one that can be converted into instructions to monitor the SQL Transaction Log.