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.
This is just untested code, from the top of my head, but it may work?
You would need to find a way to clear old sessions out of the
mutexMapbut that shouldn’t be too difficult.