Subject: PGConnection.getNotifications(0) doesn't block

From: Pawel Veselov <[email-masked]>
Date: Thu, 3 Aug 2023 16:04:08 +0200

Hello.

I'm implementing a bit that is expected to wait for published channel
notifications,
and once there are any - process them.

I'm using PGConnection.getNotifications(0), but that immediately
returns an empty
array, and doesn't block like JavaDoc says it should.

I'm on 42.6.0.

What am I missing?

Thank you.

---

From: Pawel Veselov <[email-masked]>
Date: Thu, 3 Aug 2023 16:39:18 +0200

Hello.

> array, and doesn't block like JavaDoc says it should.

The transaction state is not IDLE, that's what's missing.
But JavaDoc doesn't mention that bit.

---

From: rob stone <[email-masked]>
Date: Fri, 04 Aug 2023 18:47:43 +1000

Hello,

> array, and doesn't block like JavaDoc says it should.

Have you read the JDBC docs
https://jdbc.postgresql.org/documentation/server-prepare 
which has a section about Listen/Notify?

HTH

---

From: Pawel Veselov <[email-masked]>
Date: Fri, 4 Aug 2023 11:49:34 +0200

> which has a section about Listen/Notify?

I've read https://access.crunchydata.com/documentation/pgjdbc/42.1.1/listennotify.html
instead. I've read the Listen/Notify section in
https://jdbc.postgresql.org/documentation/server-prepare just now,
which carries identical text. I've also read the JavaDocs for the method(s).

All these sources agree or at least don't contradict that
getNotifications(0) should block without conditions,
and none mention that the connection must be in the IDLE
transaction state.

For that matter, even
https://www.postgresql.org/docs/current/sql-listen.html doesn't say
anything about the fact that notifications are only received/can be
read if the connection is in that state. It only mentions that the
 transaction that executed LISTEN must be first
committed for that connection to start seeing notifications (for
subscribed channel).

What I was effectively doing on that connection is:
BEGIN
LISTEN
COMMIT
BEGIN
UPDATE xxx;
<poll for notifications>

I've created this for tracking: https://github.com/pgjdbc/pgjdbc/issues/2937