Query Notification in SQL
In parallel programming, It became very hard in DBMS to track when records are modified (add,update,delete) of a table. We may require some notification system which notify us that a particular table record is changed so we can update the UI accordingly. Before SqlDependency, we had dependent on checked records based on some regular interval basis, it was creating unnecessary query and lots of unnecessary traffic in DBMS engine. But due to SqlDependency, it became easy to handle.
What is Query Notification?
Query Notification is a new feature which lets the application wait for the notification when the underlying query changes its result. The Database Engine uses a service broker to deliver notification messages. So, once you run the command that has notification associated with it, the Database Engine registers the notification request to it and maps it with the existing command. Hence the notification gets executed once the result is changed.
What is SQLDependecy?
SqlDependency is a special .NET class which lets us subscribe for an event and later on when the subscribed command gets notification from SQLServer, the OnChange event of the class gets called.
Which criteria needs to be considered before we start?
- column names must be defined in the query.
- Table must be used with a schema name like dbo.TableName.
- Unnamed or duplicate columns are not allowed.
- Reference to a table with Computed Columns are not allowed.
- GROUPBY, Cube, Rollup or Having are not allowed.
- Statement must not contain Pivot or Unpivot operators.
- Union, Intersect and except are not allowed.
- Statement should not contain a reference of a View.
- Query should not contain Distinct or Compute.
- NText,Text,Image Type in the query for notification are not allowed.
- Rowset functions like OpenRowset or OpenQuery are not allowed.
- Top expression is also not allowed in the query.
- Set NoCount ON will invalidate the usage of Query Notification in a stored procedure.
- Reference to server global variables (@@variableName) must also be excluded from the queries.
Before we move forward to demo, we are required to enable a service broker for a particular database which will enable us to work with the notification system.
ALTER DATABASE DatbaseName SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
- Before we start to work with SqlDependency (available in System.Data.SqlClient), We are required to register a dependency service. We can do that call the static start method of SqlDependency class. Start method accepts the argument as ConnectionString that needs to be passed as argument.
- Now we want to listen to a particular table and some of the columns on modification & changes. For that we are required to create one SqlDepedency object.
- SqlDependency object accepts SqlCommand as an argument. So, before we create SqlDepdency object, we need an SqlCommand object.
- To create SqlCommand, we need two parameters.
- SqlConnection object: This connection must have the same connection string like previously we registered with SqlDepedency.Start method.
- Command query: Require query to register with command (This query must be followed above mentioned criteria)
- After we created SqlCommand object, we can populate SqlDepedendency object by pass the SqlCommand object as an argument.
- To notify our program, we need an event. SqlDependecy has one event called OnChange which we can subscribe to and notify our program. OnChange can be registered via OnChangeEventhandler delegate. OnChangeEventHandler accepts 2 arguments. Object(Sender) & SqlNotificationEventArgs (e). So we need to create one method that has the same signature and register it with an OnChange event.
- Now we invoke the previously created SqlCommand.
- We will create one method and put all the things (from step 4 to step 6) inside the one method so we can reuse it because we need to again register the same method.
- After finishing the registration, we need to close the SqlDepedency service. We can stop the SqlDependecy using the static stop method of SqlDepedency class.
Note: Query notifications were introduced in SQL Server 2005 (9.x) and also available in Azure SQL.