r2dbc postgres驱动程序-如何从PooledConnection提取PostgresqlConnection

r2dbc postgres驱动程序-如何从PooledConnection提取PostgresqlConnection,postgresql,spring-data-r2dbc,r2dbc,r2dbc-postgresql,Postgresql,Spring Data R2dbc,R2dbc,R2dbc Postgresql,您好,我的Spring引导应用程序已使用属性文件自动配置r2dbc连接池: spring.r2dbc.url=r2dbc:pool:postgres://localhost:5432/ecom spring.r2dbc.username=xxx spring.r2dbc.password=yyy 现在我需要获取一个PostgresqlConnection实例,我这样做: this.connection=Mono.from(connectionFactory.create()).cast(Pos

您好,我的Spring引导应用程序已使用属性文件自动配置r2dbc连接池:

spring.r2dbc.url=r2dbc:pool:postgres://localhost:5432/ecom
spring.r2dbc.username=xxx
spring.r2dbc.password=yyy
现在我需要获取一个PostgresqlConnection实例,我这样做:

this.connection=Mono.from(connectionFactory.create()).cast(PostgresqlConnection.class).block();
但是因为这是一个池配置,所以我收到一个ClassCastException和以下内容 封装所需PostgresqlConnection的PooledConnection对象:

PooledConnection[PostgresqlConnection{client=io.r2dbc.postgresql.client。ReactorNettyClient@14c93774,codecs=io.r2dbc.postgresql.codec。DefaultCodecs@62a68bcb}]

我需要访问PostgresqlConnection并使用其本机功能,如通知:

PostgresqlConnection connection = …;
    Flux<Notification> listen = connection.createStatement("LISTEN mymessage")
    .execute()
    .flatMap(PostgresqlResult::getRowsUpdated)
    .thenMany(connection.getNotifications());
PostgresqlConnection连接=…;
Flux listen=connection.createStatement(“listen mymessage”)
.execute()
.flatMap(PostgresqlResult::getRowsUpdated)
.thenMany(connection.getNotifications());
问题是如何正确地从connectionFactory获取PostgresqlConnection实例?任何帮助都将不胜感激

  • 覆盖默认的ConnectionFactory

    @Bean
    @初级的
    公共连接工厂连接工厂(){
    返回新的PostgresqlConnectionFactory(
    PostgresqlConnectionConfiguration.builder()
    .host(“本地主机”)
    .数据库(“测试”)
    .用户名(“用户”)
    .密码(“密码”)
    .codecRegistrar(EnumCodec.builder().withEnum(“post_状态”,post.status.class).build())
    .build()
    );
    }
    
  • 为侦听/通知创建另一个connectionfactory

    @Bean
    @限定符(“pgConnectionFactory”)
    公共连接工厂pgConnectionFactory(){
    返回新的PostgresqlConnectionFactory(
    PostgresqlConnectionConfiguration.builder()
    .host(“本地主机”)
    .数据库(“测试”)
    .用户名(“用户”)
    .密码(“密码”)
    //.codecRegistrar(EnumCodec.builder().withEnum(“post_状态”,post.status.class).build())
    .build()
    );
    }
    
  • 我已经为第二种方法创建了一个示例,check

    启动应用程序,从
    curl
    发送hello:

    curlhttp://localhost:8080/hello
    
    在控制台中,您将看到以下消息:

    2020-09-15 16:49:20.657信息20216---[ctor-http-nio-4]发送通知:::订阅(FluxFlatMap.FlatMapMain)
    2020-09-15 16:49:20.658信息20216---[ctor-http-nio-4]发送通知:::请求(无限制)
    2020-09-15 16:49:20.666信息20216---[actor-tcp-nio-2]reactor.Flux.ConcatMap.2:onNext(NotificationResponseWrapper{name=mymessageprocessId=753参数=Hello world at 2020-09-15T16:49:20.656715600})
    2020-09-15 16:49:20.667信息20216---[actor-tcp-nio-2]com.example.demo.Listener:notification:NotificationResponseWrapper{name=mymessageprocessId=753parameter=Hello world at 2020-09-15T16:49:20.656715600}