Recent

Author Topic: PQEventmonitor not working (LISTEN/NOTIFY)  (Read 3389 times)

heejit

  • Full Member
  • ***
  • Posts: 245
PQEventmonitor not working (LISTEN/NOTIFY)
« on: July 28, 2018, 09:52:39 pm »
No notification received

Following is code

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   epq : PQEventMonitor.TPQEventMonitor;
  4.   pqcon : PQConnection.TPQConnection;
  5. begin
  6.   pqcon := PQConnection.TPQConnection.Create(nil);
  7.   pqcon.HostName:='127.0.0.1';
  8.   pqcon.DatabaseName:='db-name';
  9.   pqcon.UserName:='user-name';
  10.   pqcon.Connected:=True;
  11.  
  12.   epq := PQEventMonitor.TPQEventMonitor.Create(pqcon);
  13.   epq.Connection := pqcon;
  14.   epq.Events.Add('info');
  15.   epq.Registered:=True;
  16.   epq.OnEventAlert:=@event_handler;
  17.  
  18.  
  19. end;
  20.  
  21. procedure TForm1.event_handler(Sender: TObject; EventName: string;
  22.   EventCount: longint; var CancelAlerts: boolean);
  23. begin
  24.   writeln(EventName);
  25. end;    
  26.  

goodname

  • Sr. Member
  • ****
  • Posts: 297

heejit

  • Full Member
  • ***
  • Posts: 245
Re: PQEventmonitor not working (LISTEN/NOTIFY)
« Reply #2 on: August 07, 2018, 07:27:08 pm »
I thought this unit is Listener of postgresql Notify command

How is related to trigger?

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: PQEventmonitor not working (LISTEN/NOTIFY)
« Reply #3 on: August 07, 2018, 09:29:16 pm »
Have just looked at the Lazarus code and TPQEventMonitor uses the LISTEN and UNLISTEN commands which means your right it has nothing to do with event triggers. Have not used this feature so someone else will have to answer.

griff365

  • Newbie
  • Posts: 1
Re: PQEventmonitor not working (LISTEN/NOTIFY)
« Reply #4 on: August 28, 2021, 04:28:50 pm »
You have to explicitly set the KeepConnection option on the Postgres DB Connection to True. The default is False.

  pqcon := PQConnection.TPQConnection.Create(nil);
  pqcon.HostName:='127.0.0.1';
  pqcon.DatabaseName:='db-name';
  pqcon.UserName:='user-name';
  pqcon.KeepConnection := true;
  pqcon.Connected:=True;


The PQEventMonitor.Poll code only checks for NOTIFY events if the database is currently connected.
Setting KeepConnection := true ensures that the connection will remain open even if there are no open datasets.

This shouldn't be required, because PQEventMonitor creates it's own dedicated persistent connection anyway.
PQEventMonitor only reads the connection information from the Postgres DB Connection component for establishing it's own connection, and doesn't use it for any other purpose.


The problem in the PQEventMonitor code is here:

procedure TPQEventMonitor.Poll;
var
  notify:PpgNotify;
  CancelAlerts:boolean;
begin
  if FConnection.Connected and FRegistered and (PQconsumeInput(FDBHandle)=1) then
    begin
    CancelAlerts:=false;
    repeat
      notify:=PQnotifies(FDBHandle);
      if assigned(notify) then



I'm thinking this line of code could be changed as follows and the KeepConnection flag wouldn't need to be set.

  if FRegistered and (PQconsumeInput(FDBHandle)=1) then


 

TinyPortal © 2005-2018