spring-data-jpa中的flush方法如何工作?

时间:2018-06-20 07:30:57

标签: java spring-boot transactions spring-data flush

我有一个使用spring-boot-2.0.3spring-data-jpa-2.0.3postgresql-9.5.13的示例项目。它只有三个类:@Entity@Repository和主类。 DB只有一个表,其中包含两个字段:idname

我正在尝试将循环中的INSERT 100 000条记录插入表中并测量执行时间-为每100条记录从flush()类启用或禁用EntityManager方法

预期结果是,启用了flush()方法的执行时间比禁用了spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/twofold spring.datasource.username=postgres spring.datasource.password=postgres 的执行时间要少得多,但实际上我得到的结果却相反。


问题:我在做什么错了?


项目结构:

project structure


application.properties

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 + ";";
    }
}

User.java

package twofold.data;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

UserRepository.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));
            }
        };
    }
}

Application.java

import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

1 个答案:

答案 0 :(得分:-1)

您必须设置以下休眠属性。

    <property
        name="hibernate.jdbc.batch_size"
        value="25"
    />



    <property
        name="hibernate.order_inserts"  
        value="true"
    />

    <property
        name="hibernate.order_updates"  
        value="true"
    />

然后这样做:

int entityCount = 50;
int batchSize = 25;

EntityManager entityManager = entityManagerFactory()
    .createEntityManager();

EntityTransaction entityTransaction = entityManager
    .getTransaction();

try {
    entityTransaction.begin();

    for (int i = 0; i < entityCount; i++) {
        if (i > 0 && i % batchSize == 0) {
            entityTransaction.commit();
            entityTransaction.begin();

            entityManager.clear();
        }

        Post post = new Post(
            String.format("Post %d", i + 1)
        );

        entityManager.persist(post);
    }

    entityTransaction.commit();
} catch (RuntimeException e) {
    if (entityTransaction.isActive()) {
        entityTransaction.rollback();
    }
    throw e;
} finally {
    entityManager.close();
}