Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use saveAndFlush in springs SimpleJpaRepository?

I'm using spring-data-jpa interface CrudRepository to save large data sets in a database on a daily batch import.

@Bean
public ItemWriter<MyEntity> jpaItemWriter() {
    RepositoryItemWriter<MyEntity> writer = new RepositoryItemWriter<>();
    writer.setRepository(repository);
    writer.setMethodName("save");
    return writer;
}

The default implementation for this interface is SimpleJpaRepository, which offers a saveAndFlush() method. What is that for? Would this method be any help for me, eg regarding performance, if I run this method rather than save()?

like image 414
membersound Avatar asked Dec 11 '14 11:12

membersound


People also ask

When should we use saveAndFlush?

saveAndFlush(new Employee(2L, "Alice")); Normally, we use this method when our business logic needs to read the saved changes at a later point during the same transaction, but before the commit.

What is the difference between save and Saveflush in JPA?

On saveAndFlush , changes will be flushed to DB immediately in this command. With save , this is not necessarily true, and might stay just in memory, until flush or commit commands are issued.

What is @modifying in spring?

Using the @Modifying Annotation. The @Modifying annotation is used to enhance the @Query annotation so that we can execute not only SELECT queries, but also INSERT, UPDATE, DELETE, and even DDL queries.

What is use of @transactional?

Generally the @Transactional annotation is written at the service level. It is used to combine more than one writes on a database as a single atomic operation. When somebody call the method annotated with @Transactional all or none of the writes on the database is executed.

What is saveandflush () method in spring data JPA?

Unlike save (), the saveAndFlush () method flushes the data immediately during the execution. This method belongs to the JpaRepository interface of Spring Data JPA. Here's how we use it: Normally, we use this method when our business logic needs to read the saved changes at a later point during the same transaction but before the commit.

What is the difference between save () and saveandflush ()?

The saveAndFlush () Method Unlike save (), the saveAndFlush () method flushes the data immediately during the execution. This method belongs to the JpaRepository interface of Spring Data JPA. Here's how we use it:

What is the use of save method in spring?

The save () Method As the name depicts, the save () method allows us to save an entity to the DB. It belongs to the CrudRepository interface defined by Spring Data. Let's see how we can use it: Normally, Hibernate holds the persistable state in memory.

What is the difference between save and flush in Spring Boot?

They both are both belong to the Spring data library. save may or may not write your changes to the DB straight away. When we call saveAndFlush system are enforcing the synchronization of your model state with the DB.


1 Answers

One example would be if you were using Optimistic Locking and wanted to explicitly catch an OptimisticLockException and throw it back to the client. If the changes are only flushed to the database on transaction commit (i.e. when your transactional method returns) then you cannot do so. Explicity flushing from within your transactional method allows you to catch and rethrow/handle.

From the JPA Specification:

3.4.5 OptimisticLockException Provider implementations may defer writing to the database until the end of the transaction, when consistent with the lock mode and flush mode settings in effect. In this case, an optimistic lock check may not occur until commit time, and the OptimisticLockException may be thrown in the "before completion" phase of the commit. If the OptimisticLockException must be caught or handled by the application, the flush method should be used by the application to force the database writes to occur. This will allow the application to catch and handle optimistic lock exceptions

So, in answer to your question, it is not performance related but there may be cases when you want to explicitly flush to the database from within a transactional method.

like image 168
Alan Hay Avatar answered Oct 03 '22 22:10

Alan Hay