SQL Server Service Broker通知队列在通过SQL查询等待之后不再触发

问题描述 投票:2回答:1

(语法警告错误)我想在每次特定表发生更改时得到通知,例如插入新行。只要我不尝试通过查询而不是程序来监视SQL Server / SSMS中的队列,它就可以工作。

我创建了一个新的服务并像这样排队:

CREATE QUEUE SQLDependencyQueue;
CREATE SERVICE NamesService
ON QUEUE SQLDependencyQueue
([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]) ;

[当我现在用一个小的C#程序收听队列时,它就像一种魅力。

 private void InitializGetData()
        {
            String connectionString = "Server=DESKTOP-N943R2B\\SQLEXPRESS01;Database = DBDependency; User Id = sa;Password = admin; ";

            if (conn == null)
            {
                conn = new SqlConnection(connectionString);
            }

            if(command == null)
            {
                command = new SqlCommand(@"SELECT[id],[lade_nr],[vg_lfd_nr],[status] FROM[dbo].[t3_auftr_log_ll] where [status] = 0", conn);
            }

            if (dataToWatch == null)
            {
                dataToWatch = new DataSet();
            }

            notification = new SqlNotificationRequestRegister("WAITFOR(RECEIVE * FROM SQLDependencyQueue);", strServiceName, timeOut, connectionString);
            notification.OnChanged += NotificationOnChanged;

            getData();
        }

        void getData()
        {
            if(notification == null)
            {
                return;
            }

            dataToWatch.Clear();

            command.Notification = null;
            command.Notification = notification.NotificationRequest;

            //notification.StartSqlNotification();

            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                adapter.Fill(dataToWatch, "Order_log_ll");

                for(int i= 0; i <= dataToWatch.Tables["Order_log_ll"].Rows.Count - 1; i++)
                {
                    Console.WriteLine(String.Format("Row: {0}", dataToWatch.Tables["t3_Auftr_log_ll"].Rows[i]["vg_lfd_nr"].ToString())); //, dataToWatch.Tables["t3_Auftr_log_ll"].Rows[i]["status"].ToString()

                    using (SqlCommand cmd = new SqlCommand("update t3_auftr_log_ll set status = 1 where id = @id",conn))
                    {
                        if (conn.State != System.Data.ConnectionState.Open)
                        {
                            conn.Open();
                        }
                        cmd.Parameters.AddWithValue("@id", dataToWatch.Tables["t3_Auftr_log_ll"].Rows[i]["id"].ToString());
                        cmd.ExecuteNonQuery();
                    }
                }
            }

            notification.StartSqlNotification();
        }
private void RegisterSqlNotificationRequest()
        {
            request = new SqlNotificationRequest();
            request.UserData = new Guid().ToString();
            request.Options = String.Format("Service={0}", strServiceName);
            request.Timeout = intNotificationTimeout;

            if (OnChanged != null)
            {
                OnChanged(this, null);
            }
        }
public void Listen()
        {
            using (SqlConnection conn = new SqlConnection(strConnectionString))
            {
                using (cmd = new SqlCommand("WAITFOR(RECEIVE * FROM SQLDependencyQueue);", conn))
                {
                    if (conn.State != System.Data.ConnectionState.Open)
                    {
                        conn.Open();
                    }

                    cmd.CommandTimeout = intNotificationTimeout + 120;

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            for (int i = 0; i <= reader.FieldCount - 1; i++)
                                Debug.WriteLine(reader[i].ToString());
                        }
                    }
                }
            }

            RegisterSqlNotificationRequest();
        }

但是如果我启动程序,然后在SSMS中调用查询以查看队列中包含的内容,如下所示:

WAITFOR(RECEIVE * FROM SQLDependencyQueue);

或类似的东西:

RECEIVE conversation_handle, message_type_name, message_body  
FROM SQLDependencyQueue;

队列停止工作。我的程序正在等待通知,但是当我在表中插入内容时,SQL Server不再在此之后创建通知。

当我检查队列是否处于活动状态时:

select is_receive_enabled
from sys.service_queues
where name = N'SQLDependencyQueue';

显示为活动状态为“ 1”,但仍未收到通知。

仅当我重新启动程序并再次订阅队列时,它才能再次工作。

c# sql-server service-broker
1个回答
0
投票

如果您手动运行RECEIVE并收到一条消息,则表示您已经使用了通知。在您的C#代码中,使用了通知后,您再次订阅。如果您从SSMS使用它,则您的程序将不再订阅,因此该程序将永远等待已从队列中使用的通知,并且没人会重新订阅。

您可以检查sys.dm_qn_subscriptions查询通知的状态订阅

© www.soinside.com 2019 - 2024. All rights reserved.