spring-boot - 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>
解决方案
推荐阅读
- ios - 当应用程序为背景时如何添加图像
- python - pygame.sprite.Group.draw 不绘制图像
- java - 如何为打包安装程序(使用 Jpackage 创建)安装的 java 应用程序设置自定义安装目录?
- spring-boot - Spring REST:在 RestTemplate 与 RequestEntity 中指定 http 方法和 url
- azure - 基于读取 Odata 的 Azure 警报
- android - 为回收站视图的项目视图设置可访问性操作
- google-cloud-platform - GCP 警报过滤器不影响公开事件
- pygame - 如何返回类中立方体的位置值?
- oracle-nosql - Oracle NoSQL 数据库我在 JConsole 中看不到 JMX 统计信息
- vue.js - 如何使用 axios 和代理获取 api 数据?