3

目前我正在为 SQL 代理而苦苦挣扎。一切似乎都配置得很好,但是队列停止工作并填满了未发送的消息。代理和队列已启用。如果我删除队列和服务并重新创建它们,它会工作一段时间,但稍后会再次停止。我在 sql server 日志中没有看到任何重要错误。那么可能导致错误的原因是什么?

谢谢

队列

CREATE QUEUE [dbo].[DataChangeQueue] WITH STATUS = ON , RETENTION = OFF , ACTIVATION (  STATUS = ON , PROCEDURE_NAME = [dbo].[DataChangeQueueProc] , MAX_QUEUE_READERS = 100 , EXECUTE AS N'dbo'), POISON_MESSAGE_HANDLING (STATUS = ON)  ON [PRIMARY]

服务

CREATE SERVICE [DataChangeService]  AUTHORIZATION [dbo]  ON QUEUE [dbo].[DataChangeQueue] ([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification])

C#

    public DatabaseNotificationService()
    {
        SqlDependency.Start(m_SQLConnectionString, "DataChangeQueue");

        if (IsAccessGranted())
        {
            ConnectToDatabase();
        }
    }

    ~DatabaseNotificationService()
    {
        SqlDependency.Stop(m_SQLConnectionString, "DataChangeQueue");
    }


    private void ConnectToDatabase()
    {
        using (SqlConnection sqlConnection = new SqlConnection(m_SQLConnectionString))
        {
            sqlConnection.Open();

            using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
            {
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = GetSQLCommandText();
                sqlCommand.Notification = null;

                if (m_SQLDependency != null)
                {
                    m_SQLDependency.OnChange -= DependencyOnChange;
                    m_SQLDependency = null;
                }

                m_SQLDependency = new SqlDependency(sqlCommand, "Service=DataChangeService;Local Database=aspnetdb", 1800);
                m_SQLDependency.OnChange += DependencyOnChange;

                sqlCommand.ExecuteReader();
            }

            sqlConnection.Close();
        }
    }


    private void DependencyOnChange(object sender, SqlNotificationEventArgs e)
    {
        using (SqlConnection sqlConnection = new SqlConnection(m_SQLConnectionString))
        {
            sqlConnection.Open();

            using (SqlCommand cmd2 = sqlConnection.CreateCommand())
            {
                cmd2.CommandType = CommandType.Text;
                cmd2.CommandText = GetOnChangeSQLCommandText();

                using (SqlDataReader sqlDataReader = cmd2.ExecuteReader())
                {
                    if (sqlDataReader != null)
                    {
                        sqlDataReader.Read();

                        List<String> keys = new List<String>(m_Clients.Keys);
                        foreach (String key in keys)
                        {
                            IDatabaseNotificationCallbackContract client;
                            if (m_Clients.TryGetValue(key, out client))
                            {
                                if (((ICommunicationObject)client).State == CommunicationState.Opened)
                                {
                                    client.SendNotificationToClients(sqlDataReader.GetValue(0).ToString());
                                }
                                else
                                {
                                    m_Clients.Remove(key);
                                }
                            }                    
                        }
                    }
                }
            }

            sqlConnection.Close();
        }

        if (m_SQLDependency != null)
        {
            m_SQLDependency.OnChange -= DependencyOnChange;
            m_SQLDependency = null;
        }

        //Reconnect to database for listening to following changes.
        ConnectToDatabase();
    }
4

1 回答 1

0

当队列“停止”时它仍然启用吗?

如果没有,您可能正在处理队列中的有害消息。他们可以停止处理

我建议检查第一条消息,看看它是否对您的处理有效。

如果不是,则将其弹出。

这是我用来从消息队列中删除第一个对话(因此也是有害消息)的代码:

BEGIN TRANSACTION;

DECLARE @handle nvarchar(100);
RECEIVE TOP(1) @handle = [conversation_handle] FROM yourQueueName;

END CONVERSATION (@handle)
    WITH ERROR = 127 DESCRIPTION = N'Unable to process message.' ;
GO

COMMIT TRANSACTION;
于 2013-01-25T17:24:49.893 回答