目前我正在努力与SQL经纪人合作。一切似乎配置得很好,但队列停止工作并填满未发送的消息。代理和队列已启用。如果我删除队列和服务并重新创建它们,它会工作一段时间但稍后会再次停止。我没有在sql server日志中看到任何重要错误。那么什么可能导致错误?
由于
QUEUE
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]
SERVICE
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();
}
答案 0 :(得分:0)
队列“停止”时是否仍然启用?
如果没有,您可能正在处理队列中的poison message。他们可以停止处理
我建议检查第一条消息,看看它是否对您的处理有效。
如果不是,则将其弹出。
这是我用来删除消息队列中的第一个对话(因此也就是有害消息)的代码:
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;