运行 函数内的 oracle-db 中的数据未更新

Data not updated in oracle-db within the running funtion

从方法 callAndUpdateInB(),假设我正在调用 class B(@Component) 的 update() 方法,我在其中调用 myRepository.save() 方法来更新一些数据在数据库中,在相同的功能中,我正在执行其他一些调用......然后 return 响应返回 class A.

所以问题是当 class B 方法 update() return 响应返回到 class A 方法 callAndUpdateInB() 时,数据在数据库中得到更新。 但是当我在 class 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;
    }
}  

如果方法更新成功完成,事务将提交到数据库。

因此您看不到方法returns之前的数据。

另外save不执行insert/update语句。这也会在事务提交之前发生。

如果你想在调用saveAndFlush()之前执行语句。但是也不会提交事务,并且从另一个事务中您也不会看到此数据。

这是使用事务的 Spring 应用程序中常见的和预期的事务行为。

Propagation REQUIRED

  • 支持当前交易,如果 none 存在则创建新交易。类似于同名的 EJB 事务属性。

并在注释方法结束时保持事务未提交并保持活动状态。

如果您在请求处理的最开始调用您的 update() 两次,第一次启动事务,第二次重新使用它。如果您调用 update() 两次,一次成功,另一次失败(在唯一约束或其他情况下),这两个更改都将回滚。

开发人员通常希望交易像那样开始和结束。但在某些情况下,更改需要 committed/rolled 独立于其他更改。如果是你的情况,你可以使用 Propagation.REQUIRES_NEW:见