Re: string as lock key?

From: Scott Allen (bitmask_at_[nospam)
Date: 11/05/04


Date: Fri, 05 Nov 2004 11:21:12 -0500

Hi Lloyd:

I generally avoid locking on stirng types because they are such wierd
beasts, you never know who else might have a reference to the same
string.

Consider:

String s1 = "MyTest";
String s2 = new
       StringBuilder().Append("My").Append("Test").ToString();
String s3 = String.Intern(s2);
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.

I'm still debating if you really need a lock here. The worst that
could happen is you might end up with extra database queries if
multiple requests come in and don't find an entry in the cache. Once
the cache is warm you'll then have the overhead of many find grained
locks. The lock is relatively cheap as locks go, but how many of them
might you have?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Fri, 5 Nov 2004 20:58:06 +1100, "Lloyd Dupont"
<ld@NewsAccount.galador.net> wrote:
>in an ASP.NET page I have some static method which get value for the cache 
>or, if the cache is empty, query the database, put the value in the cache 
>and return it.
>
>because ASP.NET is thread intensive I was thinking to lock these method, 
>using a fine grained lock.
>the best lock I was thinking about was the string key in the cache!
>however I have one concern, these unique string might be used in other lock, 
>could they?
>could this be a problem ?
>
>here is my code I wonder about:
>-----------
>        public static string LogsByMonthCacheKey(string blogname)
>        {
>            return string.Intern(BLOG_KEY + ":LogsByMonth:" + blogname);
>        }
>        public static DataTable GetLogsByMonth(string blogname)
>        {
>            string ck = LogsByMonthCacheKey(blogname);
>            lock(ck) // lock with a unique string, is it alright ?
>            {
>                DataTable result = (DataTable) GetCache(ck);
>                if (result != null)
>                    return result;
>
>                SqlConnection conn = DBConnectionPool.Get();
>                try
>                {
>                    SqlCommand select = conn.CreateCommand();
>                    select.CommandText = "dbo.gb_EntryByMonth";
>                    select.CommandType = CommandType.StoredProcedure;
>                    select.Parameters.Add(new SqlParameter("@blogname", 
>blogname));
>
>                    SqlDataAdapter sda = new SqlDataAdapter(select);
>                    result = new DataTable();
>                    sda.Fill(result);
>                }
>                finally { DBConnectionPool.Let(conn); }
>
>                AddCache(ck, result);
>                return result;
>            }
>        }
> 
>


Relevant Pages

  • Re: Is this thread safe?
    ... > I was using Environment.NewLine as a ref type to lock which did work. ... > switch to Environment.OSVersion which is a demand allocated reference ... you'll get cross-application domain synchronization when putting a lock on ... For instance if you make o a string reference; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: string as lock key?
    ... Not 100% on that usage of locking on a interned string. ... is free to move around interns so compact space, so it may be possible for ... this lock not to work as expected. ... > in an ASP.NET page I have some static method which get value for the cache ...
    (microsoft.public.dotnet.framework)
  • Re: string as lock key?
    ... Not 100% on that usage of locking on a interned string. ... is free to move around interns so compact space, so it may be possible for ... this lock not to work as expected. ... > in an ASP.NET page I have some static method which get value for the cache ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Calling method/object reference
    ... The reason I want to include a reference to the locking entity, ... original holder of the lock potentially unlocking the valid lock of another ... of the cache, hence my original question. ... >> explicitly requiring the calling object to provide a reference. ...
    (comp.lang.java.programmer)
  • Re: string as lock key?
    ... you never know who else might have a reference to the same ... String s2 = new ... I'm still debating if you really need a lock here. ... multiple requests come in and don't find an entry in the cache. ...
    (microsoft.public.dotnet.framework.aspnet)