Congrats on getting SqlDependency
working (I'm not being sarcastic at all, many had failed at this).
Now is time to read Creating a Query for Notification topic on MSDN. You'll see the conditions under which queries are valid for notifications, including this requirement:
The statement must not run under READ_UNCOMMITTED or SNAPSHOT isolation levels.
I wrote about the basics of how SqlDependency
works, maybe will clear up some some misunderstandings. And, as a side node, since you're using Linq, you may be interested in LinqToCache, which provides a bridge between Linq
queries and SqlDependency
.
Another comment: do not Start()
and Stop()
your SqlDependency
nilly-willy. You'll regret it soon. Start()
is supposed to be called exactly once, during app startup, and Stop()
exactly once during app shutdown (strictly speaking, is during appdomain loading and unloading).
Now, about your problem: the isolation level that matters is the one of the notified query. That means, the query on which you attach the subscription, not the query on which you do the UPDATE
(I won't comment on the wisdom of doing UPDATE under dirty reads... or the wisdom of using dirty reads for anything). As far as I can tell, the code you show should not post the query under read_uncommitted. After you issue a SET TRANSACTION ISOLATION ...
all subsequent transactions (ergo all statements) in that session will be under that isolation level. You close the connection (via the dispose of the DataContext) and then use a different connection. Unless ... you use connection pools. Welcome to the club of innocent victims :). Connection pooling leaks isolation level changes across Close()
/Open()
boundaries. And that is your problem. There are some easy solutions:
- You can (must!) reset the isolation level explicitly after
Open()
- You can use System.Transactions scopes (my recommendation). Mandatory reading: using new TransactionScope() Considered Harmful
- Do not use connection pooling.
And while we're talking, you need to read this also: Using Tables as Queues.