标签: r2dbc-postgresql

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万
查看次数

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
查看次数

为什么会多次调用DB

我正在使用 Postgre SQL 来玩 R2DBC。我正在尝试的用例是通过 ID 以及语言、演员和类别获取电影。下面是架构

在此输入图像描述

这是ServiceImpl中对应的一段代码

@Override
public Mono<FilmModel> getById(Long id) { 
    Mono<Film> filmMono = filmRepository.findById(id).switchIfEmpty(Mono.error(DataFormatException::new)).subscribeOn(Schedulers.boundedElastic());
    Flux<Actor> actorFlux = filmMono.flatMapMany(this::getByActorId).subscribeOn(Schedulers.boundedElastic());
    Mono<String> language = filmMono.flatMap(film -> languageRepository.findById(film.getLanguageId())).map(Language::getName).subscribeOn(Schedulers.boundedElastic());
    Mono<String> category = filmMono.flatMap(film -> filmCategoryRepository
                    .findFirstByFilmId(film.getFilmId()))
            .flatMap(filmCategory -> categoryRepository.findById(filmCategory.getCategoryId()))
            .map(Category::getName).subscribeOn(Schedulers.boundedElastic());

    return Mono.zip(filmMono, actorFlux.collectList(), language, category)
            .map(tuple -> {
                FilmModel filmModel = GenericMapper.INSTANCE.filmToFilmModel(tuple.getT1());
                List<ActorModel> actors = tuple
                        .getT2()
                        .stream()
                        .map(act -> GenericMapper.INSTANCE.actorToActorModel(act))
                        .collect(Collectors.toList());
                filmModel.setActorModelList(actors);
                filmModel.setLanguage(tuple.getT3());
                filmModel.setCategory(tuple.getT4());
                return filmModel;
            });
         }
Run Code Online (Sandbox Code Playgroud)

日志显示 4 次调用拍摄

2021-12-16 21:21:20.026 DEBUG 32493 --- [ctor-tcp-nio-10] o.s.r2dbc.core.DefaultDatabaseClient     : Executing …
Run Code Online (Sandbox Code Playgroud)

project-reactor spring-webflux spring-data-r2dbc r2dbc r2dbc-postgresql

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

使用 spring-data-r2dbc 和 postgresql 从 DataIntegrityViolationException 获取失败约束名称

我在 postgresql 数据库中有一些限制(唯一的、外键...)。

我使用 spring data r2dbc 存储库:ReactiveCrudRepository

我想将DataIntegrityViolationException存储库的抛出转换为基于 的constraintNameinErrorDetails字段的自定义异常PostgresqlDataIntegrityViolationException

ExceptionFactory包含该类的类PostgresqlDataIntegrityViolationException是包私有的。所以我无法投射DataIntegrityViolationExceptionto的原因异常PostgresqlDataIntegrityViolationException

constraintName当我捕获 时,访问 的最干净的方法是什么DataIntegrityViolationException

(比解析异常消息更好的事情^^)

编辑 :

我以这个解决方案结束:

    val DataIntegrityViolationException.pgErrorDetails: ErrorDetails
        get() = when(val cause = this.cause) {
                null -> error("cause should not be null")
                else -> cause.javaClass
                        .getDeclaredField("errorDetails")
                        .apply { isAccessible = true }
                        .let { it.get(cause) as ErrorDetails }
                }
Run Code Online (Sandbox Code Playgroud)

spring-data-r2dbc r2dbc r2dbc-postgresql

5
推荐指数
1
解决办法
841
查看次数

R2DBC - PostgreSQL - 无法交换消息,因为超出了请求队列限制

图书馆:

\n
    \n
  1. r2dbc-postgresql-0.8.6.RELEASE
  2. \n
  3. r2dbc-pool-0.8.5.RELEASE
  4. \n
  5. r2dbc-spi-0.8.3.RELEASE
  6. \n
  7. postgresql-42.2.18
  8. \n
  9. 项目清单
  10. \n
\n

问题:\n我尝试使用 R2DBC (PostgreSQL) 批量插入,代码如下:

\n
@Override\npublic Flux<Long> test(List<User> users) {\n    return Mono.from(connectionFactory.create())\n    .flatMapMany(c -> Mono.from(c.beginTransaction())\n        .thenMany(Flux.fromIterable(users)\n        .map(u -> {\n            return Flux.from(c.createStatement("INSERT INTO public.users(name, age, salary) VALUES ($1, $2, $3)").returnGeneratedValues("id")\n                .bind(0, u.getName())\n                .bind(1, u.getAge())\n                .bind(2, u.getSalary()).execute());\n        })\n        .flatMap(result -> result)\n        .map(result -> result.map((row, meta) -> {\n            return row.get("id", Long.class);\n        }))\n        .flatMap(Flux::from)\n        .delayUntil(r -> c.commitTransaction())\n        .doFinally((st) -> c.close())));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

该代码将执行语句将用户插入数据库,然后获取生成的用户 ID。如果用户列表小于或等于 255,以上代码将按预期工作。当用户列表大于 255(256~)时,会出现以下异常:

\n
[5b38a8c6-2] There was an …
Run Code Online (Sandbox Code Playgroud)

postgresql spring-webflux spring-data-r2dbc r2dbc r2dbc-postgresql

5
推荐指数
1
解决办法
3415
查看次数

Spring R2dbc:有没有办法从postgresql数据库获取恒定流并处理它们?

我想将 postgresql 中的表中新创建的记录作为实时/连续流获取。可以使用spring r2dbc吗?如果是这样,我有什么选择?

谢谢

spring-webflux spring-data-r2dbc r2dbc r2dbc-postgresql

5
推荐指数
1
解决办法
722
查看次数

如何使用 spring-boot-starter-data-r2dbc 启用连接池?

我有一个具有 data-r2dbc 依赖项的 Spring Boot 应用程序。我使用 PostgreSQL 作为数据库。
所以我已经有了以下依赖项(gradle 表示法):

  • org.springframework.boot:spring-boot-starter-data-r2dbc:2.3.5.RELEASE
  • io.r2dbc:r2dbc-postgresql

我需要为 R2DBC 连接启用连接池。不幸的是,我找不到任何详尽的手册来这样做。

根据这个相当过时的发行说明,我还必须添加io.r2dbc:r2dbc-pool 并使用spring.r2dbc.pool.*属性来配置池。

此外,根据此参考,我不需要手动打开池,因为如果r2dbc-pool在类路径中找到SB 将启用它。

够不够还是我错过了什么?

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

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

postgres r2dbc 给出 ssl 错误但使用 jpa 连接

r2dbc配置:

spring:
  profiles: default   r2dbc:
  url: r2dbc:postgresql://testserver.dev.net:1234/test?ssl=true&sslmode=require
  username: test
  password: test
  connection_timeout: 20000
Run Code Online (Sandbox Code Playgroud)

jpa配置:

spring:
profiles: default
  datasource:
    url: jdbc:postgresql://testserver.dev.net:1234/test?ssl=true&sslmode=require
    username: test
    password: test
    hikari:
        connectionTimeout: 20000
        maximumPoolSize: 5
Run Code Online (Sandbox Code Playgroud)

jpa连接工作正常并返回结果,r2dbc无法连接到服务器,无法找到有效的证书

导致:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

当 r2dbc 和 ssl 关闭时,它表示 pg_hba.conf 没有主机条目。为什么它只要求带有 r2dbc 配置的证书。

与 r2dbc 的依赖关系:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency> <dependency>
        <groupId>io.r2dbc</groupId>
        <artifactId>r2dbc-postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
Run Code Online (Sandbox Code Playgroud)

对于jpa,我使用spring web starter和jpa starter,两者都是spring版本2.4.1。我被困住了,找不到这个错误的原因。欢迎任何解决方案。

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

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