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()
?
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.
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.
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.
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.
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With