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

PGConnection.getNotifications(0) doesn't block

From
Pawel Veselov
Date:
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.



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

From
Pawel Veselov
Date:
Hello.

On Thu, Aug 3, 2023 at 4:04 PM Pawel Veselov <pawel.veselov@gmail.com> wrote:
>
> 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?

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



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

From
rob stone
Date:
Hello,

On Thu, 2023-08-03 at 16:04 +0200, Pawel Veselov wrote:
> 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.
>
>

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

HTH





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

From
Pawel Veselov
Date:
> > 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.
> Have you read the JDBC docs
> https://jdbc.postgresql.org/documentation/server-prepare
> 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