c# - Can't immediately receive multiple notifications in Npgsql -
today wrote following code works 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); } } }
then following
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
create table helper(first text primary key, second integer);
create or replace function nf() returns trigger $$ 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 each row execute procedure nf();
insert helper values('first', 10), ('second', 20);
in c# project following output:
event handled: insert first 10
there's no "event handled: insert second 20". when next operation "delete helper", receive it.
why?
unless client library supports checking network socket buffered data, way receive notifications trigger other activity on socket.
many applications periodically send empty query string (""
) this.
if client library supports , not using ssl, might possible periodically call kind of checkfornotifications() function on connection. possible in pgjdbc, don't know npgsql, can advise check out documentation that.
Comments
Post a Comment