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