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 7526947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:58:35+00:00 2026-05-30T03:58:35+00:00

OK some background. I have something similar to this: class ConnectionFactory { public IConnection

  • 0

OK some background. I have something similar to this:

class ConnectionFactory 
{
    public IConnection Connect()
    {
        if (User.IsAuthenticated) {
            return InternalConnect(User.Username, null);
        }
        return null;
    }
    public IConnection Connect(string username, string password)
    {
        return InternalConnect(username, password);
    }
    private IConnection InternalConnect(string username, string password)
    {
         IConnection connection;
         var cacheKey = Session[CacheKeySessionKey] as string;

         if (!string.IsNullOrEmpty(cacheKey)) {
            connection = HttpCache[cacheKey] as IConnection;   
         }

         if (!IsGoodConnection(connection) {
            connection = MakeConnection(username, password); // very costly
            cacheKey = Session[CacheKeySessionKey] = // some key
            HttpCache[cacheKey] = connection;
         }

         return connection;
    }
    private bool IsGoodConnection(IConnection conn)
    {
        return conn != null && conn.IsConnected;
    }
}

I’m currently running into a concurrency problem where that Connect() is being called multiple times and creating multiple IConnections per request. I only need one. It is being injected using an IoC container into various instances. MakeConnnection is very costly as it spins up a WCF channel.

My question is: How can I lock the InternalConnect calls per session? I don’t think locking per request is the right way to go as multiple requests can happen per user. I certainly don’t want to lock for every call, as this will give bad performance.

I think that doing this is a bad idea:

lock(Session.SessionID)
{
   // Implementation of InternalConnect
}

Note: The username and password overload is what I call only on login.

  • 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-05-30T03:58:37+00:00Added an answer on May 30, 2026 at 3:58 am

    This is just untested code, from the top of my head, but it may work?

    // globally declare a map of session id to mutexes
    static ConcurrentDictionary<string, object> mutexMap = new ConcurrentDictionary();
    
    // now you can aquire a lock per session as follows
    object mutex = mutexMap.GetOrAdd(session.SessionId, key => new object());
    lock(mutex) 
    {
        // Do stuff with the connection
    }
    

    You would need to find a way to clear old sessions out of the mutexMap but that shouldn’t be too difficult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Something similar has been asked in maven user list but I have the situation,
Say I have a user control like this: <UserControl x:Class=StyleTest.UserControl1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:d=http://schemas.microsoft.com/expression/blend/2008
Some background: we have a windows application (c#) that locate in the system try.
Some background: In Germany (at least) invoice numbers have to follow certain rules: The
Skip to the specific question as needed. Some background: The scenario: I have a
I have background music for some songs available in both .MID and .KAR formats,
Background: I have a ListView / GridView with several columns. In some situations, only
We have to use delayed_job (or some other background-job processor) to run jobs in
I have a background thread and the thread calls some methods that update the
Hi all i have a background worker thread and some unmanaged code dlls ,

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.