java - Spring @Transactional 不适用于 CompletableFuture
问题描述
@Transactional(rollbackFor = Exception.class)
public void test(){
CompletableFuture cf1 = CompletableFuture.runAsync(()-> dbMapper.insert());
CompletableFuture cf2 = CompletableFuture.runAsync(()->{
dbMapper.insert();
throw new RuntimeException();
});
CompletableFuture.allOf(cf1, cf2).join(); // this will throw CompletionException
}
在上面的代码中,我想在抛出任何异常时回滚insert()
操作。但实际上没有回滚发生。cf1
cf2
join()
如何使它工作?或者还有其他方法吗?
解决方案
推荐阅读
- angular - 如何突出显示选定的单元格
- javascript - 如何在 React Dropdown 中添加搜索过滤器?
- encoding - 了解在上下文中编码和解码以将字符串保存在磁盘上的需求
- psql - 退出编辑器而不退出 psql
- apache-poi - 折线图 Apache POI (4.1.1) - 如何摆脱标记
- php - 通过 AWS Route53 添加多个 MX 记录 - changeResourceRecordSet - PHP
- python - 如何使用 google drive api 获取 google drive 中所有文件的名称?
- google-app-engine - Java 邮件 API 适用于本地主机但不适用于谷歌应用引擎
- c++ - 为什么构造函数只调用一次?
- android - HTML5 视频播放器在 ionic5 android 中不起作用