views:

56

answers:

5

I'm creating an application that notifies users if a new row has been added to a table in the applications database. Would it be best to create a trigger for this or just check every few seconds directly from the application? Thoughts?

A: 

Trigger is a better idea.

JonH
A: 

how will the trigger get around the need to poll?

BTW, sql-server has a notification service - but it's probably overkill

you could use a trigger to write a very short notification record to another table, and poll that instead of the main table

Steven A. Lowe
This table is updated by the FTP server once a file upload has completed, I expect only three to four inserts a day at most. Is the sql server notification service easy to understand or a learning curve not worth following for this single requirement?
madlan
@madlan - see Remus' answer ;-)
Steven A. Lowe
A: 

A trigger is the best way to catch when a new row has been added.

However, in terms of notifying the User, this depends on how you need a user to be notified. You will still need to monitor the database. Therefore you're probably best to just keep your application as is.

You could of course have a Trigger update a value elsewhere in your DB though if the initially polling count is intensive.

Robin Day
I have multiple clients that would need to be notified - If doing this client side each would need the notifcation once. (I'm using balloon tips on the apps system tray icon) I was thinking of having an extra column in the table which the application would append a unique value (such as system name) to signify this row has been "ballooned" for that client. This would also save having to store a list of processed rows locally.
madlan
A: 

A trigger is something that executes in the database, so you'd have a bit of work to do to get the trigger to send a notification back to your running application.

If you instead poll from your application (every few seconds or once a minute or whatever), you can control how often notices get sent to users. This can prevent, for example, a situation where users get thousands of emails because thousands of new rows were added.

MusiGenesis
+3  A: 

Use Query Notifications. This is the SQL Server feature that allow notifications to be actively pushed to the client when data was changed by other user. It is better known as SqlDependency. You subscribe a query and you get notified when the result set has changed (rows were added, deleted or modified).

Eg. using a LinqToCache query and reacting to a invalidation notification:

var query = (from r in ctx.table select r).AsCached(
 "table", new CachedQueryOptions() {
    OnInvalidated = (sender, args) {
      // the query was invalidated, a new row was added!
      // refresh display or notify user
    }
 });
DisplayRows(query);
Remus Rusanu