标签: r2dbc

如何使用 R2dbc 将 jsonb 从 Postgresql 提取到 Spring webflux

所以我有这个想法,这真的超出了我的想象,因为我只编程了很短一段时间,但我想构建一个反应式 Spring webflux 应用程序,将 json 端点暴露给反应前端。

当我决定在 Postgres 中使用 jsonb 格式时,问题就开始了,因为我认为我可能会从数据库一直到前端层一直使用 json。

当我尝试使用反应式 R2dbc 驱动程序使用 jsonb SELECT 表时,出现以下错误:

Caused by: java.lang.IllegalArgumentException: 3802 is not a valid object id
Run Code Online (Sandbox Code Playgroud)

我在 postgres 中有一个表,如下所示:

Column  |  Type   | Collation | Nullable |           Default
---------+---------+-----------+----------+------------------------------
 id      | integer |           | not null | generated always as identity
 details | jsonb   |           |          |
Indexes:
    "snacks_new_pkey" PRIMARY KEY, btree (id)
Run Code Online (Sandbox Code Playgroud)

因此,如果我将其作为文本提取到 Spring webflux,它就可以正常工作,因为它不再是 json。

"SELECT id, details->>'name' as NAME, details->>'price' AS PRICE, details->>'quantity' AS QUANTITY …
Run Code Online (Sandbox Code Playgroud)

java postgresql jsonb spring-webflux r2dbc

8
推荐指数
2
解决办法
9733
查看次数

使用 r2dbc 的基于多租户模式的应用程序

我正在开发一个多租户反应式应用程序,使用 Spring-Webflux + Spring-data-r2dbc 和 r2dbc 驱动程序连接到 Postgresql 数据库。多租户部分是基于模式的:每个租户一个模式。因此,根据上下文(例如用户登录),请求将命中数据库的特定架构。

我正在努力研究如何在 r2dbc 中实现这一点。理想情况下,这将是 Hibernate 使用MultiTenantConnectionProvider 的方式(参见示例 16.3)。

到目前为止我发现了什么以及我做了什么:

我可能需要找到一种方法来覆盖它,以便从上下文而不是配置中动态获取模式?

  • 否则,我可以尝试将请求中的架构指定为表前缀:

        String s = "tenant-1";
        databaseClient.execute("SELECT * FROM \"" + s + "\".\"city\"")
                .as(City.class)
                .fetch()
                .all()
    
    Run Code Online (Sandbox Code Playgroud)

但我不能再使用 SpringData,或者我需要重写每个请求以将租户作为参数传递。

任何提示/帮助表示赞赏:)

multi-tenant spring-data-r2dbc r2dbc

8
推荐指数
1
解决办法
3455
查看次数

R2dbc自定义转换器

如何将自定义转换器添加到 mu spring boot 应用程序?我的实体字段

    @CreatedDate
    @Column(value = "create_time")
    private Instant createTime;
Run Code Online (Sandbox Code Playgroud)

我的转换器是

    @Bean
    public Converter<Long, Instant> longInstantConverter() {
        return new Converter<Long, Instant>() {
            @Override
            public Instant convert(Long source) {
                return Instant.ofEpochMilli(source);
            }
        };
    }

    @Bean
    public Converter<Instant, Long> instantLongConverter() {
        return new Converter<Instant, Long>() {
            @Override
            public Long convert(@NotNull Instant source) {
                return source.toEpochMilli();
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

我有一个例外

org.springframework.data.mapping.MappingException: Could not read property @org.springframework.data.relational.core.mapping.Column(value=create_time) @org.springframework.data.annotation.CreatedDate()private java.time.Instant com.example.database.model.MyTable.createTime from result set!
.........
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of …
Run Code Online (Sandbox Code Playgroud)

spring-data-r2dbc r2dbc r2dbc-postgresql

8
推荐指数
2
解决办法
1万
查看次数

postgres r2dbc-pool 的连接池大小

我无法使用 spring-webflux 和 r2dbc (使用 r2dbc-pool driver 0.8.0.M8)打开超过 10 个连接。我的配置如下:

@Configuration
public class PostgresConfig extends AbstractR2dbcConfiguration {

  @Override
  @Bean
  public ConnectionFactory connectionFactory() {
    ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "pool")
        .option(PROTOCOL, "postgresql")
        .option(HOST, host)
        .option(USER, user)
        .option(PASSWORD, password)
        .option(DATABASE, database)
        .build());
    ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
        .maxIdleTime(Duration.ofMinutes(30))
        .initialSize(initialSize)
        .maxSize(maxSize)
        .maxCreateConnectionTime(Duration.ofSeconds(1))
        .build();
    return new ConnectionPool(configuration);
  }
}
Run Code Online (Sandbox Code Playgroud)

当我指定超过 10 个连接时,我会收到如下错误:

org.springframework.dao.DataAccessResourceFailureException: 
Failed to obtain R2DBC Connection; nested exception is 
java.util.concurrent.TimeoutException: 
Did not observe any item or terminal signal within 1000ms in 'lift' …
Run Code Online (Sandbox Code Playgroud)

java spring-webflux r2dbc

7
推荐指数
2
解决办法
2万
查看次数

r2dbc-h2 驱动程序中的 H2 控制台

我正在使用 R2DBC-H2 驱动程序,我的 UR.L 是spring.r2dbc.url=r2dbc:h2:mem:///customer

使用此配置,SpringBoot 启动正常,但是,我无法访问 h2-console。

有谁知道为什么,以及我该如何解决它?

r2dbc h2-console

7
推荐指数
1
解决办法
2416
查看次数

如何在 r2dbc-postgresql 中使用 EnumCodec

我试图使用最新版本的 r2dbc-postgresql (0.8.4) 中的 EnumCodec,但没有成功,我想知道你是否可以帮助我。

\n

我还使用 spring-data-r2dbc 版本 1.1.1。

\n

我从 GitHub 中获取了确切的示例,并在 Postgres 中创建了一个枚举类型 \xe2\x80\x9cmy_enum\xe2\x80\x9d,\n 和一个包含 \xe2\ 的表 \xe2\x80\x9csample_table\xe2\x80\x9d x80\x98name\xe2\x80\x99 (文本)和 \xe2\x80\x98value\xe2\x80\x99 (my_enum)。

\n

然后我按照例子做了:

\n

SQL:

\n
CREATE TYPE my_enum AS ENUM ('FIRST', 'SECOND');\n
Run Code Online (Sandbox Code Playgroud)\n

Java模型:

\n
enum MyEnumType {\n  FIRST, SECOND;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

编解码器注册:

\n
PostgresqlConnectionConfiguration.builder()\n.codecRegistrar(EnumCodec.builder().withEnum("my_enum", MyEnumType.class).build());\n
Run Code Online (Sandbox Code Playgroud)\n

我使用 DatabaseClient 来与数据库通信。\n我尝试使用两种方法插入:

\n
databaseClient.insert().into(SampleTable.class)\n.using(sampleTable).fetch().rowsUpdated();\n
Run Code Online (Sandbox Code Playgroud)\n

或者:

\n
databaseClient.insert().into("sample_table")\n.value("name", sampleTable.getName())\n.value("value", sampleTable.getValue())\n.then();\n
Run Code Online (Sandbox Code Playgroud)\n

其中 SampleTable 是:

\n
@Data\n@AllArgsConstructor\n@NoArgsConstructor\n@Builder\n@Table("sample_table")\n@JsonIgnoreProperties(ignoreUnknown = true)\n@JsonInclude(JsonInclude.Include.NON_NULL)\npublic class SampleTable implements Serializable {\n   private String name;\n   @Column("value")\n   @JsonProperty("value")\n …
Run Code Online (Sandbox Code Playgroud)

postgresql enums spring spring-data-r2dbc r2dbc

7
推荐指数
1
解决办法
6306
查看次数

Spring boot r2dbc transactional:注释哪个方法

我使用 spring-boot 2.4.2 和 webflux 连接到 postgres 数据库。@Transactional我在使用时观察到一种我不明白的行为。

为了展示该行为,我创建了一个示例应用程序,尝试将行添加到两个表中;表“a”和表“b”。对表“a”的插入预计会因重复键冲突而失败。鉴于使用了事务性,我预计不会将任何行添加到表“b”中。

但是,根据我使用的注释方法,@Transactional我会得到不同的结果。

如果我注释控制器方法,一切都会按预期工作,并且不会向表 B 添加任何行。

    @PostMapping("/")
    @Transactional
    public Mono<Void> postEntities() {
        return demoService.doSomething();
    }
Run Code Online (Sandbox Code Playgroud)

演示服务如下所示:

    public Mono<Void> doSomething() {
        return internal();
    }


    public Mono<Void> internal() {
        Mono<EntityA> clash = Mono.just(EntityA.builder().name("clash").build()).flatMap(repositoryA::save);
        Mono<EntityB> ok = Mono.just(EntityB.builder().name("ok").build()).flatMap(repositoryB::save);
        return ok.and(clash);
    }
Run Code Online (Sandbox Code Playgroud)

如果我将@Transactional注释从控制器移至doSomething(),那么事务仍然按预期工作。但是,如果我将@Transactional注释移至internal(),则事务将无法按预期工作。一行被添加到表“b”中。

此示例的完整代码在这里:https ://github.com/alampada/pg-spring-r2dbc-transactional

我不明白为什么将注释移至internal()方法会导致事务处理出现问题。您能解释一下吗?

spring-boot spring-data-r2dbc r2dbc r2dbc-postgresql

7
推荐指数
1
解决办法
3140
查看次数

如何修复“考虑在您的配置中定义类型为 'org.jooq.DSLContext' 的 bean。” 更新到 jOOQ 3.15.0 后

在我的 Vaadin 和 Spring Boot 应用程序中,我已从 jOOQ 3.14.12 更新到 3.15.0。此更新后,我的应用程序不再启动。这是我得到的错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.komunumo.data.service.MemberService required a bean of type 'org.jooq.DSLContext' that could not be found.


Action:

Consider defining a bean of type 'org.jooq.DSLContext' in your configuration.
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我必须定义这个 bean,因为使用 jOOQ 3.14.12 我没有必要。据我所知,这是JooqAutoConfiguration自动完成的。

spring jooq spring-data spring-boot r2dbc

7
推荐指数
1
解决办法
440
查看次数

R2DBC、Spring Jdbc、Spring Data JDBC?

关于使用 Spring JDBC,它工作得非常好,并且在使用批处理时比 JPA 有一些改进。

我很想了解当您已经拥有Spring Data JDBC 时为什么要使用 Spring Data JDBC

我很想了解当您已经拥有Spring JDBC时为什么要使用R2DBC

spring spring-jdbc spring-data-jdbc r2dbc

7
推荐指数
2
解决办法
8117
查看次数

关闭 R2DBC 池的连接

我正在尝试使用连接池配置与 PostgreSQL 数据库的 R2DBC 连接。我已遵循此文档:https://github.com/r2dbc/r2dbc-pool/blob/main/README.md

ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "pool")
        .option(PROTOCOL, "postgresql") // driver identifier, PROTOCOL is delegated as DRIVER by the pool.
        .option(HOST, "192.168.1.200")
        .option(PORT, 5433)
        .option(DATABASE, "XXX")
        .option(USER, "XXX")
        .option(PASSWORD, "XXX")
        .build());

ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
        .maxIdleTime(Duration.ofMillis(1000))
        .initialSize(10)
        .maxSize(100)
        .build();
ConnectionPool pool = new ConnectionPool(configuration);

Mono<Connection> connection = pool.create();
return Flux.from(connection)
        .flatMap(conn -> conn
                .createStatement("SELECT id, name, description FROM assortment")
                .execute())
        .flatMap(result -> result
                .map((row, rowMetadata) ->
                        new AssortmentItem(
                                row.get("id", Long.class),
                                row.get("name", String.class),
                                row.get("description", String.class)) …
Run Code Online (Sandbox Code Playgroud)

java connection-pooling r2dbc

6
推荐指数
1
解决办法
5422
查看次数