Hi, my Spring Boot application has r2dbc connection pool autoconfigured using a property file:
spring.r2dbc.url=r2dbc:pool:postgres://localhost:5432/ecom
spring.r2dbc.username=xxx
spring.r2dbc.password=yyy</code></pre>
Now I need to get a PostgresqlConnection instance and I do that this way:
this.connection = Mono.from(connectionFactory.create()).cast(PostgresqlConnection.class).block();
But because this is a pool configuration I am receiving a ClassCastException and the following PooledConnection object which wraps required PostgresqlConnection:
PooledConnection[PostgresqlConnection{client=io.r2dbc.postgresql.client.ReactorNettyClient@14c93774, codecs=io.r2dbc.postgresql.codec.DefaultCodecs@62a68bcb}]
I'd need to get to PostgresqlConnection and use its native capabilities like notifications:
PostgresqlConnection connection = …;
Flux<Notification> listen = connection.createStatement("LISTEN mymessage")
.execute()
.flatMap(PostgresqlResult::getRowsUpdated)
.thenMany(connection.getNotifications());
The question is how to properly get PostgresqlConnection instance from connectionFactory? Any help will be appreciated.
override the default ConnectionFactory.
Create another connectionfactory for listen/notify.
I have created an example for the second method, check here.
Start the application, send hello from
curl
:In the console, you will see there are some messages like the following: