【问题标题】:SQLDependency OnChange event producing an infinite loopSQLDependency OnChange 事件产生无限循环
【发布时间】:2015-02-03 03:27:41
【问题描述】:

我在使用 SQLDependency 和 SignalR Hub 时遇到问题。当启动与集线器的连接时,SQLDependencyOnChange 事件总是会触发,即使数据库中没有更改。

这是我的代码,其中包含SQLDependency

public List<NotifCenterModel> countNewTransaksi()
{
    List<NotifCenterModel> ncms = new List<NotifCenterModel>();
    command = new SqlCommand(@"SELECT Edolpuz_DB.dbo.TABEL_NOTIF_CENTER.NAMA_TABEL,Edolpuz_DB.dbo.TABEL_NOTIF_CENTER.JUMLAH_NOTIF FROM Edolpuz_DB.dbo.TABEL_NOTIF_CENTER",connect);
    try 
    {
        command.Notification = null;
        SqlDependency dependency = new SqlDependency(command);
        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
        if(connect.State == ConnectionState.Open)
            connect.Close();
        connect.Open();
        reader = command.ExecuteReader();
        while (reader.Read())
        {
            ncms.Add(new NotifCenterModel(reader[0].ToString(), int.Parse(reader[1].ToString())));
        }
        return ncms;
    }
    catch { return null; }
    finally { connect.Close(); }
}

private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
    TransHub.Show();   
}

在我的 Hub 中,代码是这样的

public class TransHub : Hub
{
    public static void Show()
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<TransHub>();
        context.Clients.All.displayStatus();
    }
}

这是我的 javascript

 $(function () {
            // Proxy created on the fly
            var job = $.connection.transHub;
            // Declare a function on the job hub so the server can invoke it
            job.client.displayStatus = function () {
                //     alert("test");
                getData();
            };

            // Start the connection
            $.connection.hub.start().done(function () {
                getData();
            }).fail(function (e) {
                alert(e);
            });

        });

function getData() {
    $.ajax({
        url: server + '/Admin/GetNotifikasi/',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            for (var i = 0; i < data.length ; i++)
            {
                if (data[i].nama_tabel == "TABEL_TRANSAKSI")
                {    
                    $('#notifTrans').text(data[i].jumlah_notif);
                }
                else if (data[i].nama_tabel == "TABEL_KONF_BAYAR")
                {
                    $('#notifBayar').text(data[i].jumlah_notif);
                }
                else if (data[i].nama_tabel == "TABEL_TESTI")
                {
                    $('#notifTesti').text(data[i].jumlah_notif);
                }
                else if (data[i].nama_tabel == "TABEL_KUSTOM_ORDER")
                {
                    $('#notifKustom').text(data[i].jumlah_notif);
                }
            }
        }
    });
}

当在connection.hub.start().done 调用getData() 时,它会不断触发并产生无限循环,但是当我不调用getData() 时,它不会在表中的数据更改时触发事件。如何解决这个问题?

【问题讨论】:

    标签: javascript c# asp.net-mvc-4 signalr-hub sqldependency


    【解决方案1】:

    dependency_OnChange,你需要检查e.Type。如果它是!= SqlNotificationType.Change,那么处理程序被调用是出于某种原因而不是数据更改。订阅本身可能失败。

    【讨论】:

    • 但是当我首先检查 e.Type 时,即使数据库发生了变化,它也不会给出任何结果。我的代码有什么问题? @jjj
    • @HandaruEriPramudiya;您是说“OnChange 事件始终在触发,即使没有更改”——对于单个订阅,您最多只会通知一次,因此每当调用 dependency_OnChange 时,订阅不再处于活动状态。但如果调用e.Type == SqlNotifcationType.Subscribe,则订阅失败。
    【解决方案2】:

    根据本站 (https://docs.microsoft.com/en-us/previous-versions/aewzkxxh(v=vs.90)),查询中的表名必须是两部分名称,如 [dbo].[TABEL_NOTIF_CENTER]。

    SELECT 语句中的投影列必须明确声明,表名必须用两部分名称限定。请注意,这意味着语句中引用的所有表都必须在同一个数据库中。

    []的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2021-01-16
      • 1970-01-01
      • 2012-06-08
      • 2013-06-05
      • 2015-12-25
      相关资源
      最近更新 更多