java - Spring JPA Transaction 问题,未保存实例
问题描述
我遇到了 sping JPA @Transaction 的问题(我认为),下面是最少的代码:
//package 1
@Transactional
public void method_m1(){
Result result = method_m2();
audit(result);
}
//package 2
@Transactional(propagation = Propagation.REQUIRED)
public Result method_m2(){
Result result = new Result()
//creating object say User(User mapped to database table users)
User u = new User();
// setting some attributes;
u = userRespositoy.save(user);
// tried with saveAndFlush no luck
result.set(u);
return result ;
}
//package 3
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void audit(Result result){
CompletableFuture.runAsync(
() -> {
//auditing user saving to db
User user = userRepository.findById(result.get().getId()).orElseThrow(() -> new
UserNotFoundException("user not found, userId :" + result.get().getId()));
});
}
在从数据库获取异常的 userId 获取新创建的用户时。
谁能解释一下这里发生了什么?
提前赞赏。
解决方案
推荐阅读
- r - 为什么在 data.frame 中创建日期变量会删除在 rstudio 中扩展变量列表的能力?
- reactjs - 在另一个 React Admin 应用程序中使用来自 React Admin 应用程序的资源
- laravel - 哪些 VUE Datatables 类似于 jQuery Datatables
- python - 我如何在没有焦点的情况下运行 tkinter 表单?
- asp.net - 显示问题 TELERIK
- python - 尝试在 jinja2 中使用“URL_FOR”时出现 404 错误,我错过了什么吗?
- gradle - 使用 WSL 运行 Kafka
- node.js - 创建 Nodejs/Express API 以连接到 React-Admin 前端框架
- c# - 如何让 JsonConvert.SerializeObject 将对象包装在 { ...}
- powershell - Powershell 命令行参数未填充