There is a project on the stack spring-boot-2.0.3 , spring-data-jpa-2.0.3 and postgresql-9.5.13 . There are three classes: @Entity , @Repository and the main class. In the database there is one table with two columns: id and name .
I add to this table 100 000 records in a loop and measure the time it takes to execute a query, including or disabling the flush() method of the EntityManager class every 100 entries.
Expected result: when the flush() method is enabled, the query execution time should be significantly less than when it is off.
Actual result: opposite indications.
Question: What am I doing wrong?
Project structure:
application.properties
spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/twofold spring.datasource.username=postgres spring.datasource.password=postgres User.java
package twofold.data; import javax.persistence.*; @Entity @Table(name = "users", schema = "public") public class User { private Long id; private String name; public User() {} public User(String name) { this.name = name; } @Id @SequenceGenerator(name = "users_id_seq", sequenceName = "users_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.TABLE, generator = "users_id_seq") @Column(name = "id", nullable = false) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "name", nullable = false, length = 50) public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "id: " + id + "; name: " + name + ";"; } } UserRepository.java
package twofold.data; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { } Application.java
package twofold; import twofold.data.User; import twofold.data.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Autowired private UserRepository userRepository; @PersistenceContext private EntityManager entityManager; @Bean public CommandLineRunner addUsers() { return new CommandLineRunner() { @Transactional public void run(String... args) throws Exception { long incoming = System.currentTimeMillis(); for (int i = 1; i <= 100000; i++) { userRepository.save(new User(i + "_name")); if (i % 100 == 0) { entityManager.flush(); entityManager.clear(); } } System.out.println("Time: " + (System.currentTimeMillis()-incoming)); } }; } } 
flush()works faster than with it, but the difference in readings has decreased. - Drakonovedspring.jpa.properties.hibernate.jdbc.batch_size. I tried both options - the effect described above. - Drakonoved