【发布时间】:2020-08-21 08:19:46
【问题描述】:
从方法 callAndUpdateInB(),假设我正在调用 B(@Component) 类的 update() 方法,其中我正在调用 myRepository.save() 方法来更新 db 中的一些数据,并且在相同的功能中我正在执行其他一些调用...然后将响应返回给 A 类。
所以问题是当 B 类方法 update() 将响应返回给 A 类方法 callAndUpdateInB() 时,数据在 db 中得到更新。 但是当我在 B() 类的更新方法中调用 myRepository.save() 时,它应该已经更新了。
为什么会这样?
仅供参考,请查看这个虚拟示例
class A{
@Autowired
B b;
public void callAndUpdateInB(String arg){
String data = b.update(arg);
// check Updates in Db (True)
// Now data is updated in db
}
}
@Component
class B{
@Transactional(
propagation = Propagation.REQUIRED
)
public String update(String arg){
MyRepository myRepository; // This is abstract class having
// imlementation for the following
// data. (MyRepositoryImpl)
String updatedData = myRepository.save(arg);
// check Updates in Db (False)
// Making some other calls, which need that updated data
// But data is not still updated in db.
// Though here the updated data field is showing that the data is updated, but it
// is not actually updated in the db.
return updatedData;
}
}
【问题讨论】:
-
事务边界在哪里?方法更新是@Transactional 吗?
-
是的,很抱歉没有提到这一点。现已更新
-
如果您使用
@Transactional,事务将在您的方法完成后提交。如果您不想从更新方法中删除@Transactionl并确保您的 Repository-save 是事务性的。
标签: oracle spring-boot microservices