pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
implementing asynchronous notifications
10+ messages / 3 participants
[nested] [flat]

* implementing asynchronous notifications
@ 2005-04-10 04:10 Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Andras Kadinger @ 2005-04-10 04:10 UTC (permalink / raw)
  To: pgsql-jdbc

Greetings everyone,

A Java beginner here, seeking the insight of more experienced.

" 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." - http://jdbc.postgresql.org/documentation/80/listennotify.html

I am looking forward to have that limitation lifted.

When we are idle and not in a transaction, the backend actually sends us
notifications right away; in this case there is no need for us to do
polling with empty queries.

Under unix you can get the fd from libpq, select() on it, and then process
notifications by PQConsumeInput() and retrieve them with PQnotifies(). -
see last paragraph at
http://www.postgresql.org/docs/current/static/libpq-notify.html

Does Java provide working infrastucture to implement this reliably? What's
needed is a reliable way to detect that bytes have arrived on our
connection socket.

I am hoping java.net.Socket.getInputStream.available() could be used. Or
even better the new select()-like functionality provided by java.nio since
1.4. Do these actually work?

If java.nio does, would the impact of choosing java.nio, and thereby
excluding pre-1.4 platforms, an acceptable way of conduct? Or is there
infrastructure in the jdbc.postgresql.org project in place to allow us to
preserve backwards-compatibility by simply omitting this feature from
pre-1.4 builds?

Thank you in advance,

Best Regards,
Andras




^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
@ 2005-04-10 05:53 ` Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Oliver Jowett @ 2005-04-10 05:53 UTC (permalink / raw)
  To: Andras Kadinger <bandit@surfnonstop.com>; +Cc: pgsql-jdbc

Andras Kadinger wrote:

> If java.nio does, would the impact of choosing java.nio, and thereby
> excluding pre-1.4 platforms, an acceptable way of conduct? 

I don't think so.. I'm pretty sure there are users that are running the
driver under 1.2 (*bsd perhaps?)

> Or is there
> infrastructure in the jdbc.postgresql.org project in place to allow us to
> preserve backwards-compatibility by simply omitting this feature from
> pre-1.4 builds?

It'll be tricky to do this from a single codebase as the NIO changes
would need to reach right down to the underlying protocol stream.. it
seems hard to localize the changes.

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
@ 2005-04-11 03:34   ` Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 03:51     ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-20 00:20     ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  0 siblings, 2 replies; 10+ messages in thread

From: Andras Kadinger @ 2005-04-11 03:34 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

On Sun, 10 Apr 2005, Oliver Jowett wrote:

> It'll be tricky to do this from a single codebase as the NIO changes
> would need to reach right down to the underlying protocol stream.. it
> seems hard to localize the changes.

Okay, then I implemented this using Socket.getInputStream().available()  
calls.

Proof of concept attached.

PGConnection.getNotifies() now processes peding Async Notifies first - no
need to send polling queries to the server anymore. The result is zero
network traffic in the idle case.

Any comments?

I am unclear as to how to handle possible protocol errors (e.g. when what
we end up reading from the connection is not an 'A'sync Notify).  
Theoretically, in a working connection this should not happen though.

Attachments:

processNotifies.patch (text/plain, 4.5K) download diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/ProtocolConnectionImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/ProtocolConnectionImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/ProtocolConnectionImpl.java 2005-01-11 09:25:43.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/ProtocolConnectionImpl.java 2005-04-11 05:07:42.000000000 +0200 @@ -58,6 +58,9 @@ public synchronized PGNotification[] getNotifications() { + try { + executor.processNotifies(); + } catch (SQLException e) {}; PGNotification[] array = (PGNotification[])notifications.toArray(new PGNotification[notifications.size()]); notifications.clear(); return array; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/QueryExecutorImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/QueryExecutorImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/QueryExecutorImpl.java 2005-02-01 08:27:54.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/QueryExecutorImpl.java 2005-04-11 05:08:52.000000000 +0200 @@ -148,6 +148,25 @@ pgStream.flush(); } + public synchronized void processNotifies() throws SQLException { + try { + while (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && pgStream.getSocket().getInputStream().available()>0) { + int c = pgStream.ReceiveChar(); + switch (c) { + case 'A': // Asynchronous Notify + receiveAsyncNotify(); + break; + default: + throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE); + } + } + } + catch (IOException ioe) + { + throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe); + } + } + private byte[] receiveFastpathResult() throws IOException, SQLException { SQLException error = null; boolean endQuery = false; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/ProtocolConnectionImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/ProtocolConnectionImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/ProtocolConnectionImpl.java 2005-01-11 09:25:44.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/ProtocolConnectionImpl.java 2005-04-11 05:07:38.000000000 +0200 @@ -59,6 +59,9 @@ public synchronized PGNotification[] getNotifications() { + try { + executor.processNotifies(); + } catch (SQLException e) {}; PGNotification[] array = (PGNotification[])notifications.toArray(new PGNotification[notifications.size()]); notifications.clear(); return array; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/QueryExecutorImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/QueryExecutorImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/QueryExecutorImpl.java 2005-02-01 08:27:54.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/QueryExecutorImpl.java 2005-04-11 05:09:12.000000000 +0200 @@ -524,6 +524,25 @@ pgStream.flush(); } + public synchronized void processNotifies() throws SQLException { + try { + while (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && pgStream.getSocket().getInputStream().available()>0) { + int c = pgStream.ReceiveChar(); + switch (c) { + case 'A': // Asynchronous Notify + receiveAsyncNotify(); + break; + default: + throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE); + } + } + } + catch (IOException ioe) + { + throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe); + } + } + private byte[] receiveFastpathResult() throws IOException, SQLException { boolean endQuery = false; SQLException error = null;
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
@ 2005-04-11 03:51     ` Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 04:22       ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Andras Kadinger @ 2005-04-11 03:51 UTC (permalink / raw)
  To: pgsql-jdbc

On Mon, 11 Apr 2005, Andras Kadinger wrote:

> I am unclear as to how to handle possible protocol errors (e.g. when what
> we end up reading from the connection is not an 'A'sync Notify).  
> Theoretically, in a working connection this should not happen though.

Yes, it could: reading the PostgreSQL protocol documentation, it says
"frontends should always be prepared to accept and display NoticeResponse
messages, even when the connection is nominally idle".

So I now added code to process Error 'N'otifications as well.

Attachments:

processNotifies.patch (text/plain, 4.9K) download diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/ProtocolConnectionImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/ProtocolConnectionImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/ProtocolConnectionImpl.java 2005-01-11 09:25:43.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/ProtocolConnectionImpl.java 2005-04-11 05:07:42.000000000 +0200 @@ -58,6 +58,9 @@ public synchronized PGNotification[] getNotifications() { + try { + executor.processNotifies(); + } catch (SQLException e) {}; PGNotification[] array = (PGNotification[])notifications.toArray(new PGNotification[notifications.size()]); notifications.clear(); return array; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/QueryExecutorImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/QueryExecutorImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/QueryExecutorImpl.java 2005-02-01 08:27:54.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/QueryExecutorImpl.java 2005-04-11 05:44:34.000000000 +0200 @@ -148,6 +148,29 @@ pgStream.flush(); } + public synchronized void processNotifies() throws SQLException { + try { + while (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && pgStream.getSocket().getInputStream().available()>0) { + int c = pgStream.ReceiveChar(); + switch (c) { + case 'A': // Asynchronous Notify + receiveAsyncNotify(); + break; + case 'N': // Error Notification + protoConnection.addWarning(receiveNotification()); + break; + + default: + throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE); + } + } + } + catch (IOException ioe) + { + throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe); + } + } + private byte[] receiveFastpathResult() throws IOException, SQLException { SQLException error = null; boolean endQuery = false; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/ProtocolConnectionImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/ProtocolConnectionImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/ProtocolConnectionImpl.java 2005-01-11 09:25:44.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/ProtocolConnectionImpl.java 2005-04-11 05:07:38.000000000 +0200 @@ -59,6 +59,9 @@ public synchronized PGNotification[] getNotifications() { + try { + executor.processNotifies(); + } catch (SQLException e) {}; PGNotification[] array = (PGNotification[])notifications.toArray(new PGNotification[notifications.size()]); notifications.clear(); return array; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/QueryExecutorImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/QueryExecutorImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/QueryExecutorImpl.java 2005-02-01 08:27:54.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/QueryExecutorImpl.java 2005-04-11 05:42:56.000000000 +0200 @@ -524,6 +524,27 @@ pgStream.flush(); } + public synchronized void processNotifies() throws SQLException { + try { + while (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && pgStream.getSocket().getInputStream().available()>0) { + int c = pgStream.ReceiveChar(); + switch (c) { + case 'A': // Asynchronous Notify + receiveAsyncNotify(); + break; + case 'N': // Notice Response (warnings / info) + SQLWarning warning = receiveNoticeResponse(); + protoConnection.addWarning(warning); + break; + default: + throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE); + } + } + } catch (IOException ioe) { + throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe); + } + } + private byte[] receiveFastpathResult() throws IOException, SQLException { boolean endQuery = false; SQLException error = null;
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 03:51     ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
@ 2005-04-11 04:22       ` Oliver Jowett <oliver@opencloud.com>
  2005-04-11 05:31         ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Oliver Jowett @ 2005-04-11 04:22 UTC (permalink / raw)
  To: Andras Kadinger <bandit@surfnonstop.com>; +Cc: pgsql-jdbc

Andras Kadinger wrote:
> On Mon, 11 Apr 2005, Andras Kadinger wrote:
> 
>>I am unclear as to how to handle possible protocol errors (e.g. when what
>>we end up reading from the connection is not an 'A'sync Notify).  
>>Theoretically, in a working connection this should not happen though.
> 
> Yes, it could: reading the PostgreSQL protocol documentation, it says
> "frontends should always be prepared to accept and display NoticeResponse
> messages, even when the connection is nominally idle".
> 
> So I now added code to process Error 'N'otifications as well.

You also need to handle errors ('E'). Try shutting down a postmaster (-m
fast) while idle connections are around -- they'll get spontaneous FATAL
errors.

> +        try {
> +            executor.processNotifies();
> +        } catch (SQLException e) {};

Don't eat the exceptions, let them propagate.

(ugh, getNotifications() does not throw SQLException. We should probably
change that..)

> +            while (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && pgStream.getSocket().getInputStream().available()>0) {

Can you move that reference following into a method on PGStream?
(hasMessagePending() or something)

The test on transaction state is a bit misleading since the connection's
transaction state should never change inside the loop. Perhaps making
that a separate test would be clearer.

I'm not sure if available() is guaranteed to work on a socket stream
everywhere (it works fine here, though), but I suppose that at worst you
get the existing behaviour where you need to send a query.

Otherwise, seems fine!

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 03:51     ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 04:22       ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
@ 2005-04-11 05:31         ` Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 05:56           ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Andras Kadinger @ 2005-04-11 05:31 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

On Mon, 11 Apr 2005, Oliver Jowett wrote:

> Andras Kadinger wrote:
> > On Mon, 11 Apr 2005, Andras Kadinger wrote:
> > 
> >>I am unclear as to how to handle possible protocol errors (e.g. when what
> >>we end up reading from the connection is not an 'A'sync Notify).  
> >>Theoretically, in a working connection this should not happen though.
> > 
> > Yes, it could: reading the PostgreSQL protocol documentation, it says
> > "frontends should always be prepared to accept and display NoticeResponse
> > messages, even when the connection is nominally idle".
> > 
> > So I now added code to process Error 'N'otifications as well.
> 
> You also need to handle errors ('E'). Try shutting down a postmaster (-m
> fast) while idle connections are around -- they'll get spontaneous FATAL
> errors.

Are you certain? The protocol documentations specifically mentions this 
case, saying it would send a NoticeResponse:

"It is possible for NoticeResponse messages to be generated due to outside 
activity; for example, if the database administrator commands a "fast" 
database shutdown, the backend will send a NoticeResponse indicating this 
fact before closing the connection. Accordingly, frontends should always 
be prepared to accept and display NoticeResponse messages, even when the 
connection is nominally idle." - 
http://www.postgresql.org/docs/8.0/static/protocol-flow.html#PROTOCOL-ASYNC

Still, I took your word on this now, and added code to handle 'E's.

> > +        try {
> > +            executor.processNotifies();
> > +        } catch (SQLException e) {};
> 
> Don't eat the exceptions, let them propagate.

The meaning behind the words "Proof of concept". :)

> (ugh, getNotifications() does not throw SQLException. We should probably
> change that..)

Agreed. I just wasn't sure changing public interfaces was the most polite
way of introducing myself. :)

Fixed now.

> > +            while (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && pgStream.getSocket().getInputStream().available()>0) {
> 
> Can you move that reference following into a method on PGStream?
> (hasMessagePending() or something)

Sure! Good idea! Done!

> The test on transaction state is a bit misleading since the connection's
> transaction state should never change inside the loop. Perhaps making
> that a separate test would be clearer.

Done!

> I'm not sure if available() is guaranteed to work on a socket stream
> everywhere (it works fine here, though), but I suppose that at worst you
> get the existing behaviour where you need to send a query.

Same here (the rationale behind my first post). I have read somewhere,
available() might not work with SSLSockets (haven't looked behind that).

> Otherwise, seems fine!

Thank you! :)

Any further comments/improvements?

If none, then hereby I'd like to submit this for inclusion.

Andras

Attachments:

processNotifies.patch (text/plain, 8.1K) download diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/PGStream.java postgresql-jdbc-8.0-310.src/org/postgresql/core/PGStream.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/PGStream.java 2005-01-11 09:25:43.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/PGStream.java 2005-04-11 07:00:42.000000000 +0200 @@ -71,6 +71,10 @@ return connection; } + public boolean hasMessagePending() throws IOException { + return connection.getInputStream().available()>0; + } + /** * Switch this stream to using a new socket. Any existing socket * is <em>not</em> closed; it's assumed that we are changing to diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/ProtocolConnection.java postgresql-jdbc-8.0-310.src/org/postgresql/core/ProtocolConnection.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/ProtocolConnection.java 2005-01-11 09:25:43.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/ProtocolConnection.java 2005-04-11 07:01:26.000000000 +0200 @@ -82,7 +82,7 @@ * @return an array of notifications; if there are no notifications, an empty * array is returned. */ - PGNotification[] getNotifications(); + PGNotification[] getNotifications() throws SQLException; /** * Retrieve and clear the chain of warnings accumulated on this connection. diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/ProtocolConnectionImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/ProtocolConnectionImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/ProtocolConnectionImpl.java 2005-01-11 09:25:43.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/ProtocolConnectionImpl.java 2005-04-11 06:58:48.000000000 +0200 @@ -57,7 +57,8 @@ } public synchronized PGNotification[] - getNotifications() { + getNotifications() throws SQLException { + executor.processNotifies(); PGNotification[] array = (PGNotification[])notifications.toArray(new PGNotification[notifications.size()]); notifications.clear(); return array; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/QueryExecutorImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/QueryExecutorImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v2/QueryExecutorImpl.java 2005-02-01 08:27:54.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v2/QueryExecutorImpl.java 2005-04-11 07:11:26.000000000 +0200 @@ -148,6 +148,31 @@ pgStream.flush(); } + public synchronized void processNotifies() throws SQLException { + if (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE) { + try { + while (pgStream.hasMessagePending()) { + int c = pgStream.ReceiveChar(); + switch (c) { + case 'A': // Asynchronous Notify + receiveAsyncNotify(); + break; + case 'E': // Error Message + throw receiveErrorMessage(); + // break; + case 'N': // Error Notification + protoConnection.addWarning(receiveNotification()); + break; + default: + throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE); + } + } + } catch (IOException ioe) { + throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe); + } + } + } + private byte[] receiveFastpathResult() throws IOException, SQLException { SQLException error = null; boolean endQuery = false; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/ProtocolConnectionImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/ProtocolConnectionImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/ProtocolConnectionImpl.java 2005-01-11 09:25:44.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/ProtocolConnectionImpl.java 2005-04-11 06:59:26.000000000 +0200 @@ -58,7 +58,8 @@ } public synchronized PGNotification[] - getNotifications() { + getNotifications() throws SQLException { + executor.processNotifies(); PGNotification[] array = (PGNotification[])notifications.toArray(new PGNotification[notifications.size()]); notifications.clear(); return array; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/QueryExecutorImpl.java postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/QueryExecutorImpl.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/core/v3/QueryExecutorImpl.java 2005-02-01 08:27:54.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/core/v3/QueryExecutorImpl.java 2005-04-11 07:11:56.000000000 +0200 @@ -524,6 +524,32 @@ pgStream.flush(); } + public synchronized void processNotifies() throws SQLException { + if (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE) { + try { + while (pgStream.hasMessagePending()) { + int c = pgStream.ReceiveChar(); + switch (c) { + case 'A': // Asynchronous Notify + receiveAsyncNotify(); + break; + case 'E': // Error Response (response to pretty much everything; backend then skips until Sync) + throw receiveErrorResponse(); + // break; + case 'N': // Notice Response (warnings / info) + SQLWarning warning = receiveNoticeResponse(); + protoConnection.addWarning(warning); + break; + default: + throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE); + } + } + } catch (IOException ioe) { + throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe); + } + } + } + private byte[] receiveFastpathResult() throws IOException, SQLException { boolean endQuery = false; SQLException error = null; diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/jdbc2/AbstractJdbc2Connection.java postgresql-jdbc-8.0-310.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2005-01-25 07:21:21.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2005-04-11 07:02:04.000000000 +0200 @@ -1023,7 +1023,7 @@ protoConnection.sendQueryCancel(); } - public PGNotification[] getNotifications() + public PGNotification[] getNotifications() throws SQLException { // Backwards-compatibility hand-holding. PGNotification[] notifications = protoConnection.getNotifications(); diff -urb postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/PGConnection.java postgresql-jdbc-8.0-310.src/org/postgresql/PGConnection.java --- postgresql-jdbc-8.0-310.src-vanilla/org/postgresql/PGConnection.java 2005-01-17 10:51:40.000000000 +0100 +++ postgresql-jdbc-8.0-310.src/org/postgresql/PGConnection.java 2005-04-11 07:03:00.000000000 +0200 @@ -27,7 +27,7 @@ * Returns null if there have been no notifications. * @since 7.3 */ - public PGNotification[] getNotifications(); + public PGNotification[] getNotifications() throws SQLException; /** * This returns the LargeObject API for the current connection.
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 03:51     ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-11 04:22       ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 05:31         ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
@ 2005-04-11 05:56           ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Oliver Jowett @ 2005-04-11 05:56 UTC (permalink / raw)
  To: Andras Kadinger <bandit@surfnonstop.com>; +Cc: pgsql-jdbc

Andras Kadinger wrote:
> On Mon, 11 Apr 2005, Oliver Jowett wrote:
> 
>>You also need to handle errors ('E'). Try shutting down a postmaster (-m
>>fast) while idle connections are around -- they'll get spontaneous FATAL
>>errors.
> 
> 
> Are you certain? The protocol documentations specifically mentions this 
> case, saying it would send a NoticeResponse:
> 
> "It is possible for NoticeResponse messages to be generated due to outside 
> activity; for example, if the database administrator commands a "fast" 
> database shutdown, the backend will send a NoticeResponse indicating this 
> fact before closing the connection. Accordingly, frontends should always 
> be prepared to accept and display NoticeResponse messages, even when the 
> connection is nominally idle." - 
> http://www.postgresql.org/docs/8.0/static/protocol-flow.html#PROTOCOL-ASYNC
> 
> Still, I took your word on this now, and added code to handle 'E's.

This is what I get from a pg_ctl stop -m fast on 8.0.0:

recvfrom(3, "E\0\0\0mSFATAL\0C57P01\0Mterminating connection due to
administrator command\0Fpostgres.c\0L2042\0RProcessInterrupts\0\0",
8192, 0, NULL, NULL) = 110

I guess the docs are out of sync..

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
@ 2005-04-20 00:20     ` Oliver Jowett <oliver@opencloud.com>
  2005-04-20 15:59       ` Re: implementing asynchronous notifications David Gagnon <dgagnon@siunik.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Oliver Jowett @ 2005-04-20 00:20 UTC (permalink / raw)
  To: Andras Kadinger <bandit@surfnonstop.com>; +Cc: pgsql-jdbc

Andras Kadinger wrote:

> PGConnection.getNotifies() now processes peding Async Notifies first - no
> need to send polling queries to the server anymore. The result is zero
> network traffic in the idle case.

I've applied your latest patch (with some cosmetic editing and a
testcase) to CVS HEAD. Thanks for implementing this!

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-20 00:20     ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
@ 2005-04-20 15:59       ` David Gagnon <dgagnon@siunik.com>
  2005-04-20 23:15         ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 10+ messages in thread

From: David Gagnon @ 2005-04-20 15:59 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

  I just read this thread. Wich seems really interesting.  Is there an 
example on how to use Async Notifies?  I look at it a couple of months 
ago and found it not sufficient for my needs.  I need to implement a 
notification mechanism to clear data from caches in my application on 
data update.

Is this change allow me to do that .. ?  That will be so nice :-)

If yes it's that possible to get a small example on how to use it.

Thanks!
/David


Oliver Jowett wrote:

>Andras Kadinger wrote:
>
>  
>
>>PGConnection.getNotifies() now processes peding Async Notifies first - no
>>need to send polling queries to the server anymore. The result is zero
>>network traffic in the idle case.
>>    
>>
>
>I've applied your latest patch (with some cosmetic editing and a
>testcase) to CVS HEAD. Thanks for implementing this!
>
>-O
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>
>  
>




^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: implementing asynchronous notifications
  2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-10 05:53 ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-11 03:34   ` Re: implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
  2005-04-20 00:20     ` Re: implementing asynchronous notifications Oliver Jowett <oliver@opencloud.com>
  2005-04-20 15:59       ` Re: implementing asynchronous notifications David Gagnon <dgagnon@siunik.com>
@ 2005-04-20 23:15         ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Oliver Jowett @ 2005-04-20 23:15 UTC (permalink / raw)
  To: David Gagnon <dgagnon@siunik.com>; +Cc: pgsql-jdbc

David Gagnon wrote:

>  I just read this thread. Wich seems really interesting.  Is there an
> example on how to use Async Notifies?  I look at it a couple of months
> ago and found it not sufficient for my needs.  I need to implement a
> notification mechanism to clear data from caches in my application on
> data update.
> 
> Is this change allow me to do that .. ?  That will be so nice :-)
> 
> If yes it's that possible to get a small example on how to use it.

Basically, you can now call PGConnection.getNotifications() and get
results without having to submit a dummy query first.

You will still need an application-level loop that periodically calls
it, though. Also, it's not guaranteed to give you notifications: you
must be not in a transaction (this is server-side behaviour), and you
must be using a Socket implementation that implements available() (SSL
connections may not do this).

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread


end of thread, other threads:[~2005-04-20 23:15 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-10 04:10 implementing asynchronous notifications Andras Kadinger <bandit@surfnonstop.com>
2005-04-10 05:53 ` Oliver Jowett <oliver@opencloud.com>
2005-04-11 03:34   ` Andras Kadinger <bandit@surfnonstop.com>
2005-04-11 03:51     ` Andras Kadinger <bandit@surfnonstop.com>
2005-04-11 04:22       ` Oliver Jowett <oliver@opencloud.com>
2005-04-11 05:31         ` Andras Kadinger <bandit@surfnonstop.com>
2005-04-11 05:56           ` Oliver Jowett <oliver@opencloud.com>
2005-04-20 00:20     ` Oliver Jowett <oliver@opencloud.com>
2005-04-20 15:59       ` David Gagnon <dgagnon@siunik.com>
2005-04-20 23:15         ` Oliver Jowett <oliver@opencloud.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox