首页 > 解决方案 > Spring webflux jpa事务

问题描述

我正在使用 spring data jpa 和 webflux,现在我遇到了事务问题。有没有办法使用 jpa 和 webflux 来管理事务?我正在使用 spring boot 2.2.1.RELEASE 版本

@Transactional
    public Mono<Entity> saveDimension(Entity entity) {
        return databaseService.call(() -> repository.saveAndFlush(entity))
                                                       .doOnNext(a -> {
                                                           throw new RuntimeException("test");
                                                       });
    }

@Transactional
@Service
public class DatabaseService {
    private Scheduler jdbcSchedluer;

    public DatabaseService(@Qualifier("jdbcScheduler") Scheduler jdbcSchedluer) {
        this.jdbcSchedluer = jdbcSchedluer;
    }

    public <T> Mono<T> call(Supplier<T> supplier) {
        return Mono.fromCallable(() -> {
            return supplier.get();
        }).subscribeOn(this.jdbcSchedluer);
    }
}

使用此代码,它将实体保存在 db 上,并且不会按预期回滚

编辑:pom

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

标签: spring-bootspring-data-jpaspring-webflux

解决方案


推荐阅读