Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ private static DataTable ParseServerEnumString(string serverInstances)
if (InstanceDetails.Count > 0)
{
dataRow = dataTable.NewRow();
dataRow[0] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.ServerNameCol) == true ?
InstanceDetails[SqlDataSourceEnumeratorUtil.ServerNameCol] : string.Empty;
dataRow[1] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.InstanceNameCol) == true ?
InstanceDetails[SqlDataSourceEnumeratorUtil.InstanceNameCol] : string.Empty;
dataRow[2] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.IsClusteredCol) == true ?
InstanceDetails[SqlDataSourceEnumeratorUtil.IsClusteredCol] : string.Empty;
dataRow[3] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.VersionNameCol) == true ?
InstanceDetails[SqlDataSourceEnumeratorUtil.VersionNameCol] : string.Empty;
dataRow[0] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.ServerNameCol, out string serverName) ?
serverName : string.Empty;
dataRow[1] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.InstanceNameCol, out string instanceName) ?
instanceName : string.Empty;
dataRow[2] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.IsClusteredCol, out string isClustered) ?
isClustered : string.Empty;
dataRow[3] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.VersionNameCol, out string versionName) ?
versionName : string.Empty;

dataTable.Rows.Add(dataRow);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ internal bool AddDNSInfo(SQLDNSInfo item)
{
if (item != null)
{
if (DNSInfoCache.ContainsKey(item.FQDN))
{

DeleteDNSInfo(item.FQDN);
}

return DNSInfoCache.TryAdd(item.FQDN, item);
DNSInfoCache[item.FQDN] = item;
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,29 +816,21 @@ private static bool AddToServerUserHash(string server, IdentityUserNamePair iden
{
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash;

if (!s_serverUserHash.ContainsKey(server))
if (!s_serverUserHash.TryGetValue(server, out identityDatabaseHash))
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.AddToServerUserHash|DEP> Hash did not contain server, adding.");
identityDatabaseHash = new Dictionary<IdentityUserNamePair, List<DatabaseServicePair>>();
s_serverUserHash.Add(server, identityDatabaseHash);
}
else
{
identityDatabaseHash = s_serverUserHash[server];
}

List<DatabaseServicePair> databaseServiceList;

if (!identityDatabaseHash.ContainsKey(identityUser))
if (!identityDatabaseHash.TryGetValue(identityUser, out databaseServiceList))
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.AddToServerUserHash|DEP> Hash contained server but not user, adding user.");
databaseServiceList = new List<DatabaseServicePair>();
identityDatabaseHash.Add(identityUser, databaseServiceList);
}
else
{
databaseServiceList = identityDatabaseHash[identityUser];
}

if (!databaseServiceList.Contains(databaseService))
{
Expand Down Expand Up @@ -869,15 +861,12 @@ private static void RemoveFromServerUserHash(string server, IdentityUserNamePair
{
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash;

if (s_serverUserHash.ContainsKey(server))
if (s_serverUserHash.TryGetValue(server, out identityDatabaseHash))
{
identityDatabaseHash = s_serverUserHash[server];

List<DatabaseServicePair> databaseServiceList;

if (identityDatabaseHash.ContainsKey(identityUser))
if (identityDatabaseHash.TryGetValue(identityUser, out databaseServiceList))
{
databaseServiceList = identityDatabaseHash[identityUser];

int index = databaseServiceList.IndexOf(databaseService);
if (index >= 0)
Expand Down Expand Up @@ -933,15 +922,17 @@ internal static string GetDefaultComposedOptions(string server, string failoverS

lock (s_serverUserHash)
{
if (!s_serverUserHash.ContainsKey(server))
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash;

if (!s_serverUserHash.TryGetValue(server, out identityDatabaseHash))
{
if (0 == s_serverUserHash.Count)
{
// Special error for no calls to start.
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.GetDefaultComposedOptions|DEP|ERR> ERROR - no start calls have been made, about to throw.");
throw SQL.SqlDepDefaultOptionsButNoStart();
}
else if (!string.IsNullOrEmpty(failoverServer) && s_serverUserHash.ContainsKey(failoverServer))
else if (!string.IsNullOrEmpty(failoverServer) && s_serverUserHash.TryGetValue(failoverServer, out identityDatabaseHash))
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.GetDefaultComposedOptions|DEP> using failover server instead\n");
server = failoverServer;
Expand All @@ -953,11 +944,9 @@ internal static string GetDefaultComposedOptions(string server, string failoverS
}
}

Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash = s_serverUserHash[server];

List<DatabaseServicePair> databaseList = null;

if (!identityDatabaseHash.ContainsKey(identityUser))
if (!identityDatabaseHash.TryGetValue(identityUser, out databaseList))
{
if (identityDatabaseHash.Count > 1)
{
Expand All @@ -976,10 +965,6 @@ internal static string GetDefaultComposedOptions(string server, string failoverS
}
}
}
else
{
databaseList = identityDatabaseHash[identityUser];
}

DatabaseServicePair pair = new(database, null);
DatabaseServicePair resultingPair = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,10 @@ internal bool AppDomainUnload(string appDomainKey)
// For each decrement, subtract from count, and delete if we reach 0.
lock (_appDomainKeyHash)
{
if (_appDomainKeyHash.ContainsKey(appDomainKey))
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int value))
{
// Do nothing if AppDomain did not call Start!
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlConnectionContainer.AppDomainUnload|DEP> _appDomainKeyHash contained AppDomainKey: '{0}'.", appDomainKey);
int value = _appDomainKeyHash[appDomainKey];
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP> _appDomainKeyHash for AppDomainKey: '{0}' count: '{1}'.", appDomainKey, value);
Debug.Assert(value > 0, "Why is value 0 or less?");

Expand All @@ -271,9 +270,9 @@ internal bool AppDomainUnload(string appDomainKey)
Debug.Assert(0 == value, "We did not reach 0 at end of loop in AppDomainUnload!");
Debug.Assert(!_appDomainKeyHash.ContainsKey(appDomainKey), "Key not removed after AppDomainUnload!");

if (_appDomainKeyHash.ContainsKey(appDomainKey))
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int remainingCount))
{
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP|ERR> ERROR - after the Stop() loop, _appDomainKeyHash for AppDomainKey: '{0}' entry not removed from hash. Count: {1}'", appDomainKey, _appDomainKeyHash[appDomainKey]);
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP|ERR> ERROR - after the Stop() loop, _appDomainKeyHash for AppDomainKey: '{0}' entry not removed from hash. Count: {1}'", appDomainKey, remainingCount);
}
}
else
Expand Down Expand Up @@ -509,10 +508,11 @@ internal void IncrementStartCount(string appDomainKey, out bool appDomainStart)
// For each increment, add to count, and create entry if not present.
lock (_appDomainKeyHash)
{
if (_appDomainKeyHash.ContainsKey(appDomainKey))
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int count))
{
_appDomainKeyHash[appDomainKey] = _appDomainKeyHash[appDomainKey] + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow 3 identical lookups under the same lock 😄

SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, _appDomainKeyHash[appDomainKey]);
count++;
_appDomainKeyHash[appDomainKey] = count;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can eliminate the increment here:

_appDomainKeyHash[appDomainKey] = count + 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would result in the incorrect count being logged below

SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, count);
}
else
{
Expand Down Expand Up @@ -1617,7 +1617,7 @@ private bool Start(
{
if (!_sqlDependencyPerAppDomainDispatchers.ContainsKey(appDomainKey))
{
_sqlDependencyPerAppDomainDispatchers[appDomainKey] = dispatcher;
_sqlDependencyPerAppDomainDispatchers.Add(appDomainKey, dispatcher);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think TryAdd() would work here and eliminate the explicit check above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryAdd doesnt exist in net classic

}
}

Expand All @@ -1636,7 +1636,7 @@ private bool Start(
SqlConnectionContainer container = null;
lock (_connectionContainers)
{
if (!_connectionContainers.ContainsKey(hashHelper))
if (!_connectionContainers.TryGetValue(hashHelper, out container))
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyProcessDispatcher.Start|DEP> {0}, hashtable miss, creating new container.", ObjectID);
container = new SqlConnectionContainer(hashHelper, appDomainKey, useDefaults);
Expand All @@ -1646,7 +1646,6 @@ private bool Start(
}
else
{
container = _connectionContainers[hashHelper];
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyProcessDispatcher.Start|DEP> {0}, hashtable hit, container: {1}", ObjectID, container.ObjectID);
if (container.InErrorState)
{
Expand Down Expand Up @@ -1712,9 +1711,8 @@ internal bool Stop(

lock (_connectionContainers)
{
if (_connectionContainers.ContainsKey(hashHelper))
if (_connectionContainers.TryGetValue(hashHelper, out SqlConnectionContainer container))
{
SqlConnectionContainer container = _connectionContainers[hashHelper];
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyProcessDispatcher.Stop|DEP> {0}, hashtable hit, container: {1}", ObjectID, container.ObjectID);
server = container.Server; // Return server, database, and queue info for use by calling SqlDependency.
database = container.Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,7 @@ internal SqlDependency LookupDependencyEntry(string id)

lock (_instanceLock)
{
if (_dependencyIdToDependencyHash.ContainsKey(id))
{
entry = _dependencyIdToDependencyHash[id];
}
else
if (!_dependencyIdToDependencyHash.TryGetValue(id, out entry))
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyPerAppDomainDispatcher.LookupDependencyEntry|DEP|ERR> ERROR - dependency ID mismatch - not throwing.");
}
Expand Down
Loading