Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 48A2E63221B for ; Fri, 11 Dec 2009 11:53:04 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 20924-01 for ; Fri, 11 Dec 2009 15:52:40 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 60A51632A2E for ; Fri, 11 Dec 2009 11:52:49 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id D14B81E044A for ; Fri, 11 Dec 2009 23:52:45 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id o2lnQep14GgY for ; Fri, 11 Dec 2009 23:52:43 +0800 (WST) Received: from [10.1.1.5] (124-169-88-117.dyn.iinet.net.au [124.169.88.117]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 92A541E0448 for ; Fri, 11 Dec 2009 23:52:43 +0800 (WST) Message-ID: <4B226ACA.4080103@postnewspapers.com.au> Date: Fri, 11 Dec 2009 23:52:42 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: PATCH: Update LISTEN/NOTIFY documentation, PGConnection JavaDoc X-Enigmail-Version: 0.95.7 Content-Type: multipart/mixed; boundary="------------070500020907060607070504" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.337 tagged_above=-10 required=5 tests=AWL=0.262, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/25 X-Sequence-Number: 20106 This is a multi-part message in MIME format. --------------070500020907060607070504 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi folks It seems the JDBC driver no longer requires a query to be issued before it will notice new notify messages and be able to process them via getNotifications() . The JDBC driver documentation doesn't reflect this. I've attached a patch that updates the documentation. It includes one thing I'm not 100% sure of, though: - It states that the need for a dummy query was removed in the 8.1 driver, but I'm not sure that it was in fact 8.1 (and have no idea what the build number was). Before I went CVS-history-diving I thought I'd ask and see if anybody knew when the change was. In addition to correcting the confusion, I've added @see links to the Pg LISTEN/NOTIFY docs, added a note about older versions requiring a dummy select, noted that calling getNotifications() doesn't create network traffic, and fixed an unrelated place where "8.1" should've read "8.1 or later". -- Craig Ringer --------------070500020907060607070504 Content-Type: text/x-patch; name="PgJDBC-notify.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="PgJDBC-notify.diff" Index: doc/pgjdbc.xml =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/doc/pgjdbc.xml,v retrieving revision 1.39 diff -u -r1.39 pgjdbc.xml --- doc/pgjdbc.xml 20 Jun 2009 15:19:40 -0000 1.39 +++ doc/pgjdbc.xml 11 Dec 2009 15:36:53 -0000 @@ -2294,11 +2294,16 @@ - - A key limitation of the JDBC driver is that it - cannot receive asynchronous notifications and must poll the - backend to check if any notifications were issued. - + + In PgJDBC drivers earlier than 8.1, + notifications were not processed until a statement was next executed. + Dummy statements like SELECT 1; were used to force + the driver to check for notifications. If your application must be compatible with + such old versions, you will need to check + org.postgresql.Driver.getVersion() and + issue dummy statements before getNotifications() + calls if necessary. + @@ -2346,13 +2351,8 @@ public void run() { while (true) { try { - // issue a dummy query to contact the backend - // and receive any pending notifications. - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT 1"); - rs.close(); - stmt.close(); - + // Check the connection's read buffer for any unprocessed + // notifications and return them. org.postgresql.PGNotification notifications[] = pgconn.getNotifications(); if (notifications != null) { for (int i=0; i<notifications.length; i++) { @@ -2425,7 +2425,7 @@ avoid these changes in query results, but the V3 protocol is only available beginning with server version 7.4. Enabling server-prepared statements will have no affect when connected to a 7.3 server or when - explicitly using the V2 protocol to connect to a 7.4 server. + explicitly using the V2 protocol to connect to a 7.4 or later server. Index: org/postgresql/PGConnection.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/PGConnection.java,v retrieving revision 1.17 diff -u -r1.17 PGConnection.java --- org/postgresql/PGConnection.java 1 Jul 2009 05:00:39 -0000 1.17 +++ org/postgresql/PGConnection.java 11 Dec 2009 15:36:53 -0000 @@ -22,10 +22,21 @@ public interface PGConnection { /** - * This method returns any notifications that have been received - * since the last call to this method. - * Returns null if there have been no notifications. + *

This method returns any notifications that have been received + * since the last call to this method.

+ * + *

getNotifications() does not cause any network + * communication with the server. It only checks a local receive + * buffer for notifications, and is a fairly cheap operation.

+ * + *

Version Compatibility Note: In driver versions older + * than 8.1 it was necessary to execute an SQL query via the JDBC + * driver before the driver would notice waiting notifications.

+ * + * @see PostgreSQL documentation for the LISTEN and NOTIFY commands * @since 7.3 + * + * @return Array of notifications or null if there have been no notifications */ public PGNotification[] getNotifications() throws SQLException; Index: org/postgresql/PGNotification.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/PGNotification.java,v retrieving revision 1.12 diff -u -r1.12 PGNotification.java --- org/postgresql/PGNotification.java 8 Jan 2008 06:56:26 -0000 1.12 +++ org/postgresql/PGNotification.java 11 Dec 2009 15:36:53 -0000 @@ -10,7 +10,9 @@ package org.postgresql; /** - * This interface defines the public PostgreSQL extension for Notifications + * This interface defines the public PostgreSQL extension for Notifications. + * @see PGConnection#getNotifications() + * @see PostgreSQL documentation for the LISTEN and NOTIFY commands */ public interface PGNotification { --------------070500020907060607070504--