0

everyone.

So, I have a WebFlux application which uses a PostgreSQL database as a queue (LISTEN/NOTIFY). It works more or less fine; the problem is that at some point the DB notifications just stop arriving.

The notifications listener is set up in the following way (R2DBC with r2dbc-postgresql driver is used):

val config = PostgresqlConnectionConfiguration.builder()
    // setting up host, DB name, host, etc
    .build()

val factory = PostgresqlConnectionFactory(config)
val connection = Mono.from(factory.create())
    .cast(PostgresqlConnection::class.java)
    .block()!!

// I need to listen a couple of channels
connection
    .createBatch()
    .add("LISTEN channel_1;")
    .add("LISTEN channel_2;")
    .execute()
    .flatMap { it.rowsUpdated }
    .log("listen::")
    .blockLast()

connection.notifications
    .delayElements(Duration.ofSeconds(1))
    .log("notifications::")
    .subscribe(
        { notification ->
            log.debug("Notification received ${notification.parameter}")
        },
        { error ->
            log.error("Connection error: $error")
        }
     )

This code may work fine for hours or even days, but then all of a sudden the listener just stops receiving new notifications. I can see in the logs, that the NOTIFY channel_1,'...' queries are executed, but neither "Notification received..." nor "Connection error" messages follow them (and the rest of the handling logic is not invoked) as if the listener has silently disconnected. Is there any kind of a best practice to handle this?

Thank you in advance.

Alex Kiselev
  • 552
  • 2
  • 7
  • 22

0 Answers0