#How to optimize when we use JpaRepository<T,T>'s ...AndFlush() methods?

1 messages · Page 1 of 1 (latest)

nimble violet
#

How to optimize when we use JpaRepository<T,T>'s ...AndFlush() methods? For example, SampleJpaRepository.save() vs SampleJpaRepository.saveAndFlush()?

I understand that the ...AndFlush() methods tend to be used for:

- Immediate synchronization with the database/To ensure that the entity is persisted right away
- Ensure that subsequent operations (like CRUD) reflect the latest state of the database

I also understand that database synchronization (flushing) is triggered when we:

- Commit a transaction
- Manually flush using a `flush()` or `...AndFlush()` operation
- Execute a query **(not sure what type of query triggers this?)**

Further, I am aware that Spring tends to defer database synchronization (flushing) by batching multiple operations and waiting until absolutely necessary before it flushes, in order to reduce the number of database round trips.

So then how do we know when to saveAndFlush()?

For example, I see that CommandLineRunner methods usually ALWAYS perform .saveAndFlush(). Why? Is it because they lack a @Transactional service method to do the flushing for them?

compact gardenBOT
#

<@&987246584574140416> please have a look, thanks.

compact gardenBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

nimble violet
#

Bump

nimble violet
#

Bump

earnest cypress
#

not sure I understand the question, but generally the regular save method is enough

#

The way it works in spring is that when you're using @Transactional, changhes are flushed at the end of a transaction

#

this is the most efficent way because you don't have as many round trips to the database

#

when you're using saveAndFlush this flushes changes to the db straight away and ususally you only do this when you want the data in the db the very moment you are inserting data

#

but it's slow to do that, especially if you have batches

#

for most cases use save

#

For example, I see that CommandLineRunner methods usually ALWAYS perform .saveAndFlush(). Why? Is it because they lack a @Transactional service method to do the flushing for them?

#

for the most part, you're right, there's probably no transaction around it

#

So then how do we know when to saveAndFlush()?
all i can answer to this is, you'll know when you need it

#

I guess a real world example could be something like purchasing something from a website. Let says you have 1 product left in stock, when that's sold, you might want to do a saveAndFlush so you can immediatly let people know that you're all sold out

nimble violet
earnest cypress
#

round trip is making a database call

nimble violet
earnest cypress
#

no, the actual call to the database

#

here's an example

#

lets say you're adding 5 users to the db

#

when doing save

#

you make 1 call to the database to save all the 5 users

#

so imagine a single insert/update statement

#

when you do saveAndFlush

#

you do

#

update user1;

#

update user2;

#

... update user5;

#

each time you're running a seperate query 5 times

#

which means you're sending data to the database 5x instead of just once

nimble violet
#

How different is a database call from the actual sql statement 🤔

#

I was imagining 5 update queries but then i think thats not what you mean

earnest cypress
#

yeah same thing

#

here's another example

#
1
2
3
4
5```
#

I said ^ 1,2,3,4,5 in 1 single message on discord

#

1

#

2

#

3

#

4

#

5

#

this is done 1 by 1

#

which one was quicker?

nimble violet
#

But save() also persists entities one by one

Do you mean saveAll()?

earnest cypress
#

it doesn't - it caches them internally

#

but it's not saved to the database

nimble violet
#

Ohh not until a sync event is triggered

earnest cypress
#

yeah

#

i.e. flush

nimble violet
#

Or commit

earnest cypress
#

yea

nimble violet
#

There's a third thing that triggers a sync

#

Some query thing

#

But i dont rly understand it

earnest cypress
#

i answered in your other question 🙂