3

我试图使用 Spring Boot Webflux 和 postgres R2DBC 驱动程序从数据库中获取对象列表,但我收到一条错误消息:

value ignored org.springframework.transaction.reactive.TransactionContextManager$NoTransactionInContextException: No transaction in context Context1{reactor.onNextError.localStrategy=reactor.core.publisher.OnNextFailureStrategy$ResumeStrategy@7c18c255}

似乎所有 DatabaseClient 操作都需要包装到一个事务中。

我尝试了 spring-boot-data 和 r2db 之间依赖关系的不同组合,但并没有真正起作用。

版本:

        <spring-boot.version>2.2.0.RC1</spring-boot.version>
        <spring-data-r2dbc.version>1.0.0.BUILD-SNAPSHOT</spring-data-r2dbc.version>
        <r2dbc-releasetrain.version>Arabba-M8</r2dbc-releasetrain.version>

依赖项:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-r2dbc</artifactId>
  <version>${spring-data-r2dbc.version}</version>
</dependency>
<dependency>
  <groupId>io.r2dbc</groupId>
  <artifactId>r2dbc-postgresql</artifactId>
</dependency>
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-bom</artifactId>
    <version>${r2dbc-releasetrain.version}</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>
</dependencyManagement>
fun findAll(): Flux<Game> {
    val games = client
        .select()
        .from(Game::class.java)
        .fetch()
        .all()
        .onErrorContinue{ throwable, o -> System.out.println("value ignored $throwable $o") }
    games.subscribe()
    return Flux.empty()
}

@Table("game")
data class Game(@Id val id: UUID = UUID.randomUUID(),
                @Column("guess") val guess: Int = Random.nextInt(500))

Github 仓库:https ://github.com/odfsoft/spring-boot-guess-game/tree/r2dbc-issue

我希望读取操作不需要 @Transactional 或运行查询而不手动包装到事务上下文中。

更新: 在多次尝试多个版本后,我设法找到了一个有效的组合:

<spring-data-r2dbc.version>1.0.0.BUILD-SNAPSHOT</spring-data-r2dbc.version>
<r2dbc-postgres.version>0.8.0.RC2</r2dbc-postgres.version>

依赖项:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-r2dbc</artifactId>
  <version>${spring-data-r2dbc.version}</version>
</dependency>
<dependency>
  <groupId>io.r2dbc</groupId>
  <artifactId>r2dbc-postgresql</artifactId>
  <version>${r2dbc-postgres.version}</version>
</dependency>
<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-bom</artifactId>
    <version>Arabba-RC2</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>
</dependencyManagement>

由于以下原因,r2dbc 的版本符号似乎从 1.0.0.M7 变为 0.8.x:

但是更新到最新版本后出现了一个新问题,即需要一个事务来运行查询,如下所示:

更新配置:

@Configuration
class PostgresConfig : AbstractR2dbcConfiguration() {

    @Bean
    override fun connectionFactory(): ConnectionFactory {
        return PostgresqlConnectionFactory(
            PostgresqlConnectionConfiguration.builder()
                .host("localhost")
                .port(5432)
                .username("root")
                .password("secret")
                .database("game")
                .build())
    }

    @Bean
    fun reactiveTransactionManager(connectionFactory: ConnectionFactory): ReactiveTransactionManager {
        return R2dbcTransactionManager(connectionFactory)
    }

    @Bean
    fun transactionalOperator(reactiveTransactionManager: ReactiveTransactionManager) =
        TransactionalOperator.create(reactiveTransactionManager)

}

询问:

    fun findAll(): Flux<Game> {
        return client
            .execute("select id, guess from game")
            .`as`(Game::class.java)
            .fetch()
            .all()
            .`as`(to::transactional)
            .onErrorContinue{ throwable, o -> System.out.println("value ignored $throwable $o") }
            .log()
    }

免责声明这并不意味着在生产中使用!!仍然在GA之前。

4

0 回答 0