Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hH9Hh-0000BA-Ju for pgsql-jdbc@arkaria.postgresql.org; Thu, 18 Apr 2019 15:49:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hH9Hg-0007SE-75 for pgsql-jdbc@arkaria.postgresql.org; Thu, 18 Apr 2019 15:49:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hH8L6-0003Rd-Fl for pgsql-jdbc@lists.postgresql.org; Thu, 18 Apr 2019 14:48:36 +0000 Received: from mail2.dalim.com ([145.253.191.74]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hH8L0-0000c8-46 for pgsql-jdbc@postgresql.org; Thu, 18 Apr 2019 14:48:32 +0000 Received: from localhost (localhost [127.0.0.1]) by mail2.dalim.com (Postfix) with ESMTP id 9E167ED4ED; Thu, 18 Apr 2019 16:48:26 +0200 (CEST) Received: from mail2.dalim.com ([127.0.0.1]) by localhost (mail2.dalim.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id ZFcqskiBr_tR; Thu, 18 Apr 2019 16:48:24 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail2.dalim.com (Postfix) with ESMTP id C7B3AEC002; Thu, 18 Apr 2019 16:48:24 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail2.dalim.com C7B3AEC002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dalim.com; s=C5CD85AE-9875-11E7-8F4E-7B36B4C25526; t=1555598904; bh=DVGquL9kZYNBxHHsQPQwRBB5yWeZNuJ42ecyePqmMLs=; h=Date:From:To:Message-ID:MIME-Version; b=fvjjW/mUPKzBpJ5Me1SntlpWLdd+/02RLlioylw2yZyhZ8TGdKy1hV/DTWchoE3rK 90JD3vPU/QVy79z5h3y6E05db6iLQmUbP/GbYdaVSbZeL5DEfV/+NbFtKHGwm2lPoa q+FgSLqk4cTCjpxZUq9vu51it99tjaE3fadfGPM8= X-Virus-Scanned: amavisd-new at mail2.dalim.com Received: from mail2.dalim.com ([127.0.0.1]) by localhost (mail2.dalim.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Xc23swHJIScQ; Thu, 18 Apr 2019 16:48:24 +0200 (CEST) Received: from mail2.dalim.com (localhost [127.0.0.1]) by mail2.dalim.com (Postfix) with ESMTP id 87BA8D8900; Thu, 18 Apr 2019 16:48:24 +0200 (CEST) Date: Thu, 18 Apr 2019 16:48:24 +0200 (CEST) From: Philippe Ebersohl To: Dave Cramer Cc: List Message-ID: <1209056006.4825828.1555598904371.JavaMail.zimbra@dalim.com> In-Reply-To: References: <1058674020.3669693.1550249725415.JavaMail.zimbra@dalim.com> <1000052273.5516163.1551264468429.JavaMail.zimbra@dalim.com> <15704019.5681554.1551347564337.JavaMail.zimbra@dalim.com> <1703112628.790711.1552297274774.JavaMail.zimbra@dalim.com> Subject: Re: A method to asynchronously LISTEN ? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_89938dc9-8855-4215-96e8-2ca1b7a6bee1" X-Mailer: Zimbra 8.8.11_GA_3780 (ZimbraWebClient - SAF12 (Mac)/8.8.11_GA_3780) Thread-Topic: A method to asynchronously LISTEN ? Thread-Index: SDtCy4pqFEbsCHx7G26j2EkMCydWBA== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_89938dc9-8855-4215-96e8-2ca1b7a6bee1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hello, This is not what was expected, when using the pgjdbc-ng driver there is no need to poll. ( [ https://github.com/impossibl/pgjdbc-ng | https://github.com/impossibl/pgjdbc-ng ] ) Do you think this feature could be added to the pgsql-jdbc driver ? If not, our devs will have to use both drivers, the pgjdbc-ng for the asynchronous notifications without polling and the pgsql-jdbc for the dbcp. Best regards, Philippe Ebersohl I got an example, and the declaration is much alike: public void listen() throws SQLException { PGNotificationListener listener = new PGNotificationListener() { public void notification ( int processId , String channelName , String payload ) { System. out .println( "Received Notification: " + processId + ", " + channelName + ", " + payload ); } public void closed () { // initiate reconnection & restart listening } }; Statement statement = conn .createStatement() ; statement .execute( "LISTEN " + QNAME ); statement .close(); conn .addNotificationListener( listener ); System. out .println( "Wait for events" ); } But there is no SELECT involved. The main just does: postgresNG .listen(); Object lock = new Object(); synchronized ( lock ) { lock . wait (); } And when NOTIFY is issued from pgAdmin for example, the answer is printed immediately by the java program. From: "Dave Cramer" To: "Philippe Ebersohl" Cc: "List" Sent: Wednesday, 10 April, 2019 16:20:39 Subject: Re: A method to asynchronously LISTEN ? Philippe, Here's what's going on. The FE/BE protocol is somewhat synchronous and the NOTIFY is not being read until some other activity occurs on the channel. If you modify your test to do the following it works fine. public void testNotify() throws SQLException, InterruptedException { AtomicBoolean flag = new AtomicBoolean( false ); Statement stmt = conn .createStatement(); ((PGConnection) conn ).addNotificationListener( new PGNotificationListener() { @Override public void notification(PGNotification notification) { System. out .println( "Notification Received" ); flag .set( true ); } }); stmt.executeUpdate( "LISTEN mynotification" ); for ( int i= 0 ;i< 30 ; i++) { stmt.execute( "select 1" ); Thread. sleep ( 1000 ); } stmt.close(); Dave Cramer [ mailto:davec@postgresintl.com | davec@postgresintl.com ] [ http://www.postgresintl.com/ | www.postgresintl.com ] On Wed, 13 Mar 2019 at 10:37, Dave Cramer < [ mailto:pg@fastcrypt.com | pg@fastcrypt.com ] > wrote: On Mon, 11 Mar 2019 at 05:41, Philippe Ebersohl < [ mailto:philippe.ebersohl@dalim.com | philippe.ebersohl@dalim.com ] > wrote: BQ_BEGIN Hello, could you pleas advise what is wrong in our testing ? We retrieved the git sources, build them. Modified the testNotify() as shown below. When performing "NOTIFY mynotification" from an SQL client, we do not see the " Notification received" message as expected. Thank you. Regards, Philippe Ebersohl Hello Phillipe, I will look at this as soon as possible. I am currently busy with other things. Dave Cramer [ mailto:davec@postgresintl.com | davec@postgresintl.com ] [ http://www.postgresintl.com/ | www.postgresintl.com ] BQ_BEGIN From: "Philippe Ebersohl" < [ mailto:philippe.ebersohl@dalim.com | philippe.ebersohl@dalim.com ] > To: "Dave Cramer" < [ mailto:pg@fastcrypt.com | pg@fastcrypt.com ] > Cc: "David G. Johnston" < [ mailto:david.g.johnston@gmail.com | david.g.johnston@gmail.com ] >, "List" < [ mailto:pgsql-jdbc@postgresql.org | pgsql-jdbc@postgresql.org ] > Sent: Thursday, 28 February, 2019 10:52:44 Subject: Re: A method to asynchronously LISTEN ? Hello, we retrieved the sources. After starting the testNotify() below, we execute from pgadmin : notify mynotification But the listener is not notified. Did we miss something ? public void testNotify() throws SQLException, InterruptedException { final AtomicBoolean flag = new AtomicBoolean(false); Statement stmt = conn.createStatement(); ((org.postgresql.PGConnection)conn).addNotificationListener(new org.postgresql.PGNotificationListener() { @Override public void notification(org.postgresql.PGNotification notification) { flag.set(true); System.out.println("Notification received"); } }); stmt.executeUpdate("LISTEN mynotification"); // stmt.executeUpdate("NOTIFY mynotification"); Thread.sleep(30000); org.postgresql.PGNotification[] notifications = conn.unwrap(org.postgresql.PGConnection.class).getNotifications(); //assertTrue(flag.get()); //assertNotNull(notifications); //assertEquals(1, notifications.length); //assertEquals("mynotification", notifications[0].getName()); //long endMillis = System.currentTimeMillis(); //long runtime = endMillis - startMillis; //assertNull("There have been notifications, although none have been expected.",notifications); //assertTrue("We didn't wait long enough! runtime=" + runtime, runtime > 450); stmt.close(); } Regards. Philippe Ebersohl From: "Dave Cramer" < [ mailto:pg@fastcrypt.com | pg@fastcrypt.com ] > To: "Philippe Ebersohl" < [ mailto:philippe.ebersohl@dalim.com | philippe.ebersohl@dalim.com ] > Cc: "David G. Johnston" < [ mailto:david.g.johnston@gmail.com | david.g.johnston@gmail.com ] >, "List" < [ mailto:pgsql-jdbc@postgresql.org | pgsql-jdbc@postgresql.org ] > Sent: Wednesday, 27 February, 2019 11:53:59 Subject: Re: A method to asynchronously LISTEN ? On Wed, 27 Feb 2019 at 05:47, Philippe Ebersohl < [ mailto:philippe.ebersohl@dalim.com | philippe.ebersohl@dalim.com ] > wrote: BQ_BEGIN Hello, I do not think there is a need for something like conn.listenSync(). We were just intrigued about the test. In fact the current proposition should just work fine and we would like to test it. How could we have access to a postgresql-*.jdbc.jar that includes this feature ? Regards, Philippe BQ_END I just rebased it over the current code [ https://github.com/pgjdbc/pgjdbc/pull/579 | https://github.com/pgjdbc/pgjdbc/pull/579 ] You should be able to build it manually using the commandline instructions to get the pull git checkout -b davecramer-notify master git pull [ https://github.com/davecramer/pgjdbc.git | https://github.com/davecramer/pgjdbc.git ] notify then use maven to build it Dave Cramer [ mailto:davec@postgresintl.com | davec@postgresintl.com ] [ http://www.postgresintl.com/ | www.postgresintl.com ] BQ_BEGIN From: "David G. Johnston" < [ mailto:david.g.johnston@gmail.com | david.g.johnston@gmail.com ] > To: "Dave Cramer" < [ mailto:pg@fastcrypt.com | pg@fastcrypt.com ] > Cc: "Philippe Ebersohl" < [ mailto:philippe.ebersohl@dalim.com | philippe.ebersohl@dalim.com ] >, "List" < [ mailto:pgsql-jdbc@postgresql.org | pgsql-jdbc@postgresql.org ] > Sent: Tuesday, 26 February, 2019 22:17:12 Subject: Re: A method to asynchronously LISTEN ? On Tue, Feb 26, 2019 at 12:04 PM Dave Cramer < [ mailto:pg@fastcrypt.com | pg@fastcrypt.com ] > wrote: BQ_BEGIN On Tue, 26 Feb 2019 at 12:29, David G. Johnston < [ mailto:david.g.johnston@gmail.com | david.g.johnston@gmail.com ] > wrote: BQ_BEGIN The question being asked is whether executeUpdate method performs synchronous execution of the listener function queue during its execution. This after sending the command to the server and the server completing execution of the NOTIFY. This assumes that the server places the notification on the channel for pick-up immediately (which will not be the case within a transaction). While the client is, IIRC, synchronous, thus the local order of operations can be controlled, the server is asynchronous and thus this test is exposed to timing issues. Maybe we need something like conn.listenSync() that blocks until a notification payload is received on the connection...? BQ_END kind of defeats the asynchronous aspect, no ? BQ_BEGIN BQ_BEGIN BQ_BEGIN BQ_END BQ_END BQ_END BQ_END You'd still want an asynchronous API for people but JavaScript introduced the "async/await" feature for a reason. In this case making it "sync" instead of coding up wait loop seems desirable. That said its quite possible I'm missing some existing feature as I haven't used this API at all. David J. [ https://euroduo.dalim.com/ ] BQ_END [ https://euroduo.dalim.com/ ] [ https://euroduo.dalim.com/ ] BQ_END BQ_END --=_89938dc9-8855-4215-96e8-2ca1b7a6bee1 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hello,

This is not what wa= s expected, when using the pgjdbc-ng driver there = is no need to poll.
(https://github.com/impossibl/pgjdbc-ng<= span data-mce-style=3D"font-size: 10pt;" style=3D"font-size: 10pt;">)

Do you think this feature could be added = to the pgsql-jdbc driver ?
If not, our devs will have to use both= drivers, the pgjdbc-ng for the asynchronous notifications without polling = and the pgsql-jdbc for the dbcp.
 
Best regards,

Philippe Eber= sohl

I got an example, and the declaration is much alike:


public void listen() throws SQLExc= eption {

PGNotificationListener listener&= nbsp;=3D new PGNotificationListener(= ) { 

public<= /span> void notification(int processI= d, String channel= Name, String payl= oad) {

System.out.println("Received Notificat= ion: " + processId +&n= bsp;", " +<= span class=3D"Apple-converted-space"> channelName + ", "&nbs= p;+ payload); 

}

= public voi= d = closed() { 

// initiate reconnectio= n & restart listening

}

};

State= ment statement =3D conn.createStatement();

<= span data-mce-style=3D"color: #6a3e3e;" style=3D"color: rgb(106, 62, 62);">= statement.execute("LISTEN "+QNAME);

state= ment.close();

conn.addNotificationListener(liste= ner);

System.out.println("Wait for events");

}


But there is no SELECT involved.
The main just does:

 postgresNG.liste= n();

Object lo= ck =3D new Object();

synchronized (lock)

{

lock.wait();

}


And when NOTIFY is issued from pgAdmin for ex= ample, the answer is printed immediately by the java program.




From: "Dave Cramer" <pg@fastcrypt.com>
= To: "Philippe Ebersohl" <philippe.ebersohl@dalim.com>
Cc: <= /b>"List" <pgsql-jdbc@postgresql.org>
Sent: Wednesday, 10 A= pril, 2019 16:20:39
Subject: Re: A method to asynchronously LISTE= N ?

Philippe,
Here's what's going on. The FE/BE protocol is so= mewhat synchronous and the NOTIFY is not being read until some other activi= ty occurs on the channel.

If you modify your test to do the f= ollowing it works fine.

public void testNotify() throws SQLException, InterruptedException {
Atom= icBoolean flag =3D new = AtomicBoolean(fa= lse);
Statement stmt =3D conn.createStatement();
((PGConnection)conn).addNotific= ationListener(new PGNotificationListener() {
= @Override
public void
notificat= ion(PGNotification notification) {
System.out.println("Notification Received");
flag.set(true);
}
= });
stmt.executeUpdate("LISTEN mynotification");
for (int i=3D0= ;i<30; i++) {
stmt.ex= ecute("select 1"= );
Thread.sleep(1000);
}
stmt.close();

On Wed, 1= 3 Mar 2019 at 10:37, Dave Cramer <pg@fastcrypt.com> wrote:


On M= on, 11 Mar 2019 at 05:41, Philippe Ebersohl <philippe.ebersohl@dalim.com> w= rote:

Hello,=
could you pleas advise what is wrong in our testing ?

<= div>We retrieved the git sources, build them.
Modified the testNo= tify() as shown below.
When performing  "NOTIFY mynotification"  from an SQL client, we do not see  the "Notification received" message as expected.

Thank you= .

Regards,
<= div>

Philippe Ebersohl 



Hello Phillipe,

I will look at this as soon as = possible. I am currently busy with other things.


 



From: "Philippe Ebersohl" <philippe.ebersohl@dalim.com&g= t;
To: "Dave Cramer" <pg@fastcrypt.com>
Cc: "David G. Johnston" &l= t;david.g.j= ohnston@gmail.com>, "List" <pgsql-jdbc@postgresql.org>
Sent: Thursday, 28 February, 2019 10:52:44
Subject: Re: A method to as= ynchronously LISTEN ?

Hello,
we retrieved the s= ources.


After starting the testNotify() below, we execute from pgadmi= n : notify mynotification

But the listener is not notified.

Did we miss somethin= g ?



public void testNotify() throws SQLException, InterruptedException= {
            final Atomic= Boolean flag =3D new AtomicBoolean(false);
     &nbs= p;      Statement stmt =3D conn.createStatement();
&= nbsp;           ((org.postgresql.PG= Connection)conn).addNotificationListener(new org.postgresql.PGNotificationL= istener() {
           &nbs= p;  @Override
          &nb= sp;   public void notification(org.postgresql.PGNotification noti= fication) {
           &nbs= p;    flag.set(true);

        &nb= sp;           System.out.print= ln("Notification received");

        &nb= sp;     }
        &nbs= p;   });
          &nb= sp; stmt.executeUpdate("LISTEN mynotification");
//    &n= bsp;       stmt.executeUpdate("NOTIFY mynotificati= on");

            Threa= d.sleep(30000);
           =
            org.postgresq= l.PGNotification[] notifications =3D conn.unwrap(org.postgresql.PGConnectio= n.class).getNotifications();
         = ;   //assertTrue(flag.get());
      &= nbsp;     //assertNotNull(notifications);
  &nb= sp;         //assertEquals(1, notifications.l= ength);
            //asser= tEquals("mynotification", notifications[0].getName());
   = ;         //long endMillis =3D System.current= TimeMillis();
            /= /long runtime =3D endMillis - startMillis;
     &nbs= p;      //assertNull("There have been notifications, al= though none have been expected.",notifications);
    &nbs= p;       //assertTrue("We didn't wait long enough!= runtime=3D" + runtime, runtime > 450);

     =        stmt.close();
     &= nbsp;    }


Regards.

Philippe Eber= sohl 





<= div>

On Wed, 27 Feb= 2019 at 05:47, Philippe Ebersohl <philippe.ebersohl@dalim.com> wrote:
<= /div>
Hello,
I d= o not think there is a need for something like conn.listenSync().
We were just intrigued about the test.

=
In= fact the current proposition should just work fine and we would like to te= st it.
How could we have access to a postgresql-*.jdbc.jar that includes this = feature ?

Regards,
Philippe 

I just rebased it over the current code https://github.com/pgjdbc/pgjdbc= /pull/579
You should be able to build it manually using the c= ommandline instructions to get the pull 

git ch= eckout -b davecramer-notify master

then use maven to build it

<= div>
<= span style=3D"font-family:arial,helvetica,sans-serif">

=
From: "David G. Johnston" <david.g.johnston@g= mail.com>
To: "Dave Cramer" <pg@fastcrypt.com>
Cc: "Philipp= e Ebersohl" <philippe.ebersohl@dalim.com>, "List" <pgsql-jdbc@postgresql.org>=
Sent: Tuesday, 26 February, 2019 22:17:12
Subject: Re:= A method to asynchronously LISTEN ?

On Tue, Feb 26, 2019 at 12:= 04 PM Dave Cramer <pg@fastcrypt.com> wrote:
<= div dir=3D"ltr">

On Tue, 26 Feb 2019 at 12:29, David G. Johnston <david.g.johnst= on@gmail.com> wrote:
The question being asked is whether executeUpdate = method performs synchronous execution of the listener function queue during= its execution.
=
This after = sending the command to the server and the server completing execution of th= e NOTIFY.  This assumes that the server places the notification on the= channel for pick-up immediately (which will not be the case within a trans= action).

While the client i= s, IIRC, synchronous, thus the local order of operations can be controlled,= the server is asynchronous and thus this test is exposed to timing issues.=   Maybe we need something like conn.listenSync() that blocks until a n= otification payload is received on the connection...?

kind of defeats the asynchronous aspect, no ?


You'd still want an asynchronous API for people but JavaScrip= t introduced the "async/await" feature for a reason.  In this case mak= ing it "sync" instead of coding up wait loop seems desirable.  That sa= id its quite possible I'm missing some existing feature as I haven't used t= his API at all.
=
David J.
=

3D"Visit


3D"Visit

3D"Visit

--=_89938dc9-8855-4215-96e8-2ca1b7a6bee1--