Home > Net >  Execution Order of Flush() after a @Transactional method is executed
Execution Order of Flush() after a @Transactional method is executed

Time:10-28

@Transactional
mymethod(){
    repo.saveAll(Large data);
    repo.save(small data); //updates db that the large data is written
}

What I'm experiencing:
After the transactional method's successful execution the two datas are flushed async. That is the small data is written before the large data.

What I want:
After the transactional method's successful execution the saveAll's data should be flushed first then the save's data should be flushed.

Is my understanding of, flush() of saveAll and save happening in async is correct? If yes, then how can I order the execution of flush of saveAll and save.

CodePudding user response:

i guess you set attribute of "small data" (for example smallData.setField1(value1)) earlier than "large data".

CodePudding user response:

In the JpaRepository, there should be a method saveAllAndFlush:

@Transactional
mymethod(){
    repo.saveAllAndFlush(Large data);
    repo.save(small data); //updates db that the large data is written
}

The difference is that it it will flush the changes to the database without committing the transaction.

  • Related