// Remove from commandToDependenciesHash all references to the passed dependency.
        private void RemoveDependencyFromCommandToDependenciesHash(SqlDependency dependency)
        {
            IntPtr hscp;

            Bid.NotificationsScopeEnter(out hscp, "<sc.SqlDependencyPerAppDomainDispatcher.RemoveDependencyFromCommandToDependenciesHash|DEP> %d#, SqlDependency: %d#", ObjectID, dependency.ObjectID);
            try
            {
                lock (this)
                {
                    List <string> notificationIdsToRemove = new List <string>();
                    List <string> commandHashesToRemove   = new List <string>();

                    foreach (KeyValuePair <string, DependencyList> entry in _notificationIdToDependenciesHash)
                    {
                        DependencyList dependencies = entry.Value;
                        if (dependencies.Remove(dependency))
                        {
                            Bid.NotificationsTrace("<sc.SqlDependencyPerAppDomainDispatcher.RemoveDependencyFromCommandToDependenciesHash|DEP> Removed SqlDependency: %d#, with ID: '%ls'.\n", dependency.ObjectID, dependency.Id);
                            if (dependencies.Count == 0)
                            {
                                // this dependency was the last associated with this notification ID, remove the entry
                                // note: cannot do it inside foreach over dictionary
                                notificationIdsToRemove.Add(entry.Key);
                                commandHashesToRemove.Add(entry.Value.CommandHash);
                            }
                        }

                        // same SqlDependency can be associated with more than one command, so we have to continue till the end...
                    }

                    Debug.Assert(commandHashesToRemove.Count == notificationIdsToRemove.Count, "maps should be kept in sync");
                    for (int i = 0; i < notificationIdsToRemove.Count; i++)
                    {
                        // cleanup the entry outside of foreach
                        // do it inside finally block to avoid ThreadAbort exception interrupt this operation
                        try
                        { }
                        finally
                        {
                            _notificationIdToDependenciesHash.Remove(notificationIdsToRemove[i]);
                            // VSTS 216991: cleanup the map between the command hash and associated notification ID
                            _commandHashToNotificationId.Remove(commandHashesToRemove[i]);
                        }
                    }

                    Debug.Assert(_notificationIdToDependenciesHash.Count == _commandHashToNotificationId.Count, "always keep these maps in sync!");
                }
            }
            finally
            {
                Bid.ScopeLeave(ref hscp);
            }
        }
Example #2
0
        // Remove from commandToDependenciesHash all references to the passed dependency.
        private void RemoveDependencyFromCommandToDependenciesHash(SqlDependency dependency)
        {
            long scopeID = SqlClientEventSource.Log.NotificationScopeEnterEvent("<sc.SqlDependencyPerAppDomainDispatcher.RemoveDependencyFromCommandToDependenciesHash|DEP> {0}, SqlDependency: {1}", ObjectID, dependency.ObjectID);

            try
            {
                lock (_instanceLock)
                {
                    List <string> notificationIdsToRemove = new List <string>();
                    List <string> commandHashesToRemove   = new List <string>();

                    foreach (KeyValuePair <string, DependencyList> entry in _notificationIdToDependenciesHash)
                    {
                        DependencyList dependencies = entry.Value;
                        if (dependencies.Remove(dependency))
                        {
                            SqlClientEventSource.Log.NotificationTraceEvent("<sc.SqlDependencyPerAppDomainDispatcher.RemoveDependencyFromCommandToDependenciesHash|DEP> Removed SqlDependency: {0}, with ID: '{1}'.", dependency.ObjectID, dependency.Id);
                            if (dependencies.Count == 0)
                            {
                                // this dependency was the last associated with this notification ID, remove the entry
                                // note: cannot do it inside foreach over dictionary
                                notificationIdsToRemove.Add(entry.Key);
                                commandHashesToRemove.Add(entry.Value.CommandHash);
                            }
                        }

                        // same SqlDependency can be associated with more than one command, so we have to continue till the end...
                    }

                    Debug.Assert(commandHashesToRemove.Count == notificationIdsToRemove.Count, "maps should be kept in sync");
                    for (int i = 0; i < notificationIdsToRemove.Count; i++)
                    {
                        // cleanup the entry outside of foreach
                        _notificationIdToDependenciesHash.Remove(notificationIdsToRemove[i]);
                        // Cleanup the map between the command hash and associated notification ID
                        _commandHashToNotificationId.Remove(commandHashesToRemove[i]);
                    }

                    Debug.Assert(_notificationIdToDependenciesHash.Count == _commandHashToNotificationId.Count, "always keep these maps in sync!");
                }
            }
            finally
            {
                SqlClientEventSource.Log.NotificationScopeLeaveEvent(scopeID);
            }
        }
Example #3
0
        private void RemoveDependencyFromCommandToDependenciesHash(SqlDependency dependency)
        {
            IntPtr ptr;

            Bid.NotificationsScopeEnter(out ptr, "<sc.SqlDependencyPerAppDomainDispatcher.RemoveDependencyFromCommandToDependenciesHash|DEP> %d#, SqlDependency: %d#", this.ObjectID, dependency.ObjectID);
            try
            {
                lock (this)
                {
                    List <string> list  = new List <string>();
                    List <string> list3 = new List <string>();
                    foreach (KeyValuePair <string, DependencyList> pair in this._notificationIdToDependenciesHash)
                    {
                        DependencyList list2 = pair.Value;
                        if (list2.Remove(dependency))
                        {
                            Bid.NotificationsTrace("<sc.SqlDependencyPerAppDomainDispatcher.RemoveDependencyFromCommandToDependenciesHash|DEP> Removed SqlDependency: %d#, with ID: '%ls'.\n", dependency.ObjectID, dependency.Id);
                            if (list2.Count == 0)
                            {
                                list.Add(pair.Key);
                                list3.Add(pair.Value.CommandHash);
                            }
                        }
                    }
                    for (int i = 0; i < list.Count; i++)
                    {
                        try
                        {
                        }
                        finally
                        {
                            this._notificationIdToDependenciesHash.Remove(list[i]);
                            this._commandHashToNotificationId.Remove(list3[i]);
                        }
                    }
                }
            }
            finally
            {
                Bid.ScopeLeave(ref ptr);
            }
        }
Example #4
0
        // Remove from commandToDependenciesHash all references to the passed dependency.
        private void RemoveDependencyFromCommandToDependenciesHash(SqlDependency dependency)
        {
            lock (_instanceLock)
            {
                List <string> notificationIdsToRemove = new List <string>();
                List <string> commandHashesToRemove   = new List <string>();

                foreach (KeyValuePair <string, DependencyList> entry in _notificationIdToDependenciesHash)
                {
                    DependencyList dependencies = entry.Value;
                    if (dependencies.Remove(dependency))
                    {
                        if (dependencies.Count == 0)
                        {
                            // this dependency was the last associated with this notification ID, remove the entry
                            // note: cannot do it inside foreach over dictionary
                            notificationIdsToRemove.Add(entry.Key);
                            commandHashesToRemove.Add(entry.Value.CommandHash);
                        }
                    }

                    // same SqlDependency can be associated with more than one command, so we have to continue till the end...
                }

                Debug.Assert(commandHashesToRemove.Count == notificationIdsToRemove.Count, "maps should be kept in sync");
                for (int i = 0; i < notificationIdsToRemove.Count; i++)
                {
                    // cleanup the entry outside of foreach
                    _notificationIdToDependenciesHash.Remove(notificationIdsToRemove[i]);
                    // Cleanup the map between the command hash and associated notification ID
                    _commandHashToNotificationId.Remove(commandHashesToRemove[i]);
                }

                Debug.Assert(_notificationIdToDependenciesHash.Count == _commandHashToNotificationId.Count, "always keep these maps in sync!");
            }
        }