无法立即在Npgsql中接收多个通知 [英] Can't immediately receive multiple notifications in Npgsql

查看:86
本文介绍了无法立即在Npgsql中接收多个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我编写了以下代码,这些代码可用于名为Npgsql的PostgreSQL C#库:

Today i wrote the following code that works with PostgreSQL C# library named Npgsql:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;

namespace PostgreSQLNotificationsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=my_password;Database=helper_db.psql;SyncNotification=true"))
            {
                conn.Notification += OnNotification;
                conn.Open();
                using (var command = new NpgsqlCommand("listen notifytest;", conn))
                {
                    command.ExecuteNonQuery();
                }
                Console.ReadLine();
            };
        }

        private static void OnNotification(object sender, NpgsqlNotificationEventArgs e)
        {
            Console.WriteLine("event handled: " + e.AdditionalInformation);
        }
    }
}

然后我执行以下操作


createdb.exe -h 127.0.0.1 -p 5432 -U postgres -W helper_db.psql

createdb.exe -h 127.0.0.1 -p 5432 -U postgres -W helper_db.psql

psql.exe -h 127.0.0.1 -p 5432 -U postgres -W helper_db.psql

psql.exe -h 127.0.0.1 -p 5432 -U postgres -W helper_db.psql

创建表助手(第一个文本主键,第二个整数);

create table helper(first text primary key, second integer);



CREATE OR REPLACE FUNCTION nf() RETURNS TRIGGER AS $$
BEGIN
    PERFORM pg_notify('notifytest', format('INSERT %s %s', NEW.first, NEW.second));
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER any_after AFTER INSERT OR UPDATE OR DELETE ON helper FOR EACH ROW EXECUTE PROCEDURE nf();




插入辅助值('first',10),(' second',20);

insert into helper values('first', 10), ('second', 20);

在C#项目中,我得到以下输出:

In C# project i get the following output:


事件已处理:INSERT前10个

event handled: INSERT first 10

没有事件已处理:INSERT其次20个。仅当我执行从助手中删除之类的下一个操作时,我才会收到它。

There's no "event handled: INSERT second 20". Only when i do the next operation like "delete from helper", i receive it.

为什么?

推荐答案

除非您的客户端库支持检查网络套接字中是否有缓冲数据,否则接收通知的唯一方法是在套接字上触发其他活动。

Unless your client library supports checking the network socket for buffered data, the only way to receive notifications is to trigger some other activity on the socket.

许多应用程序会定期发送空的查询字符串( )来执行此操作。

Many applications periodically send an empty query string ("") to do this.

如果客户端库支持它,并且您没有使用SSL,则可能可以定期在连接上调用某种checkForNotifications()函数。这在PgJDBC中是可能的,但是我不知道nPgSQL,所以我只建议您查看该文档。

If the client library supports it and you are not using SSL, it might be possible to periodically call some kind of checkForNotifications() function on the connection. This is possible in PgJDBC, but I don't know nPgSQL, so I can only advise you to check out the documentation for that.

这篇关于无法立即在Npgsql中接收多个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆