I’m not 100% sure how you test that the data is not written immediately to the database, but I’m guessing you are checking with a separate database connection.
This probably means that your data in deed is written to the database, but not committed and therefore not visible for other sessions.
Make sure that the transaction scope is only inside the loop you are using for writing.
For this the method containing the for loop should not have a
@Transactional annotation or in any other way included in a transaction, while the call to
save is within a transaction.
saveAndFlush is not necessary since the transaction commits and this will trigger a flush anyway.
If the call to
save the only interaction with the database Spring will actually automatically wrap it in a transaction, because repositories are annotated with
@Transactional out of the box. Otherwise you’ll need to use the transaction support of Spring to achieve this.
CLICK HERE to find out more related problems solutions.