Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:24:18+00:00 2026-06-02T16:24:18+00:00

I’m thinking of caching permissions for every user on our application server. Is it

  • 0

I’m thinking of caching permissions for every user on our application server. Is it a good idea to use a SqlCacheDependency for every user?

The query would look like this

SELECT PermissionId, PermissionName From Permissions Where UserId = @UserId

That way I know if any of those records change then to purge my cache for that user.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:24:19+00:00Added an answer on June 2, 2026 at 4:24 pm

    If you read how Query Notifications work you’ll see why createing many dependency requests with a single query template is good practice. For a web app, which is implied by the fact that you use SqlCacheDependency and not SqlDependency, what you plan to do should be OK. If you use Linq2Sql you can also try LinqToCache:

    var queryUsers = from u in repository.Users 
            where u.UserId = currentUserId 
            select u;
    var user= queryUsers .AsCached("Users:" + currentUserId.ToString());
    

    For a fat client app it would not be OK. Not because of the query per-se, but because SqlDependency in general is problematic with a large number of clients connected (it blocks a worker thread per app domain connected):

    SqlDependency was designed to be used in ASP.NET or middle-tier
    services where there is a relatively small number of servers having
    dependencies active against the database. It was not designed for use
    in client applications, where hundreds or thousands of client
    computers would have SqlDependency objects set up for a single
    database server.

    Updated

    Here is the same test as @usr did in his post. Full c# code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using DependencyMassTest.Properties;
    using System.Threading.Tasks;
    using System.Threading;
    
    namespace DependencyMassTest
    {
        class Program
        {
            static volatile int goal = 50000;
            static volatile int running = 0;
            static volatile int notified = 0;
            static int workers = 50;
            static SqlConnectionStringBuilder scsb;
            static AutoResetEvent done = new AutoResetEvent(false);
    
            static void Main(string[] args)
            {
                scsb = new SqlConnectionStringBuilder(Settings.Default.ConnString);
                scsb.AsynchronousProcessing = true;
                scsb.Pooling = true;
    
                try
                {
                    SqlDependency.Start(scsb.ConnectionString);
    
                    using (var conn = new SqlConnection(scsb.ConnectionString))
                    {
                        conn.Open();
    
                        using (SqlCommand cmd = new SqlCommand(@"
    if object_id('SqlDependencyTest') is not null
        drop table SqlDependencyTest
    
    create table SqlDependencyTest (
        ID int not null identity,
        SomeValue nvarchar(400),
        primary key(ID)
    )
    ", conn))
                        {
                            cmd.ExecuteNonQuery();
                        }
                    }
    
                    for (int i = 0; i < workers; ++i)
                    {
                        Task.Factory.StartNew(
                            () =>
                            {
                                RunTask();
                            });
                    }
                    done.WaitOne();
                    Console.WriteLine("All dependencies subscribed. Waiting...");
                    Console.ReadKey();
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                }
                finally
                {
                    SqlDependency.Stop(scsb.ConnectionString);
                }
            }
    
            static void RunTask()
            {
                Random rand = new Random();
                SqlConnection conn = new SqlConnection(scsb.ConnectionString);
                conn.Open();
    
                SqlCommand cmd = new SqlCommand(
    @"select SomeValue
        from dbo.SqlDependencyTest
        where ID = @id", conn);
                cmd.Parameters.AddWithValue("@id", rand.Next(50000));
    
                SqlDependency dep = new SqlDependency(cmd);
                dep.OnChange += new OnChangeEventHandler((ob, qnArgs) =>
                {
                    Console.WriteLine("Notified {3}: Info:{0}, Source:{1}, Type:{2}", qnArgs.Info, qnArgs.Source, qnArgs.Type, Interlocked.Increment(ref notified));
                });
    
                cmd.BeginExecuteReader(
                    (ar) =>
                    {
                        try
                        {
                            int crt = Interlocked.Increment(ref running);
                            if (crt % 1000 == 0)
                            {
                                Console.WriteLine("{0} running...", crt);
                            }
                            using (SqlDataReader rdr = cmd.EndExecuteReader(ar))
                            {
                                while (rdr.Read())
                                {
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine(e.Message);
                        }
                        finally
                        {
                            conn.Close();
                            int left = Interlocked.Decrement(ref goal);
    
                            if (0 == left)
                            {
                                done.Set();
                            }
                            else if (left > 0)
                            {
                                RunTask();
                            }
                        }
                    }, null);
    
            }
    
        }
    }
    

    After 50k subscriptions are set up (takes about 5 min), here are the stats io of a single insert:

    set statistics time on
    insert into Test..SqlDependencyTest (SomeValue) values ('Foo');
    
    SQL Server parse and compile time: 
       CPU time = 0 ms, elapsed time = 0 ms.
    
     SQL Server Execution Times:
       CPU time = 16 ms,  elapsed time = 16 ms.
    

    Inserting 1000 rows takes about 7 seconds, which includes firing several hundred notifications. CPU utilization is about 11%. All this is on my T420s ThinkPad.

    set nocount on;
    go
    
    begin transaction
    go
    insert into Test..SqlDependencyTest (SomeValue) values ('Foo');
    go 1000
    
    commit
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.