使用springboot+JPA,第一次保存太慢

问题描述 投票:0回答:1

我使用springboot+JPA+Hibernate来插入数据。但是第一条数据插入速度太慢,后面就变快了。我想消除这种情况,并使所有数据插入速度更快,包括第一个。有什么办法可以实现这一点吗?也许可以通过配置改变或者一些初始化操作来实现。以下是我的配置、所有测试代码和测试结果。我已经将它们上传到 GitHub[https://github.com/MayflyShake/heart-test]。

我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>heart</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>heart-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <log4j2.version>2.17.0</log4j2.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>heart-test-0.0.1-SNAPSHOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的应用程序.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/tt_test?useSSL=false&serverTimezone=UTC
    username: root
    password: root
    hikari:
      maximum-pool-size: 10
      minimum-idle: 5
      idle-timeout: 30000
      pool-name: SpringBootHikariCP
      auto-commit: true
      connection-timeout: 30000
      connection-test-query: SELECT 1
  jpa:
    hibernate:
      database-platform: org.hibernate.dialect.MySQLDialect
      ddl-auto: update
    show-sql: true

logging:
  config: config/log4j2-spring.xml

我的实体:

@Slf4j
@Getter
@Setter
@Entity
@Table(name = "test_order")
public class TestOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id", nullable = false)
    private Integer orderId;

    @Column(name = "create_time")
    private String createTime;

    @Column(name = "params", columnDefinition = "text")
    private String params;

}

我的道:

@Repository
public interface TestOrderDao extends JpaRepository<TestOrder, Integer> {
}

我的测试主要代码:

@SpringBootApplication
public class HeartApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(HeartApplication.class, args);
        TestOrderDao testOrderDao = context.getBean(TestOrderDao.class);
        String param = "{\"param1\":\"value1\"}";
        TestOrder t1 = new TestOrder();
        t1.setCreateTime("2024-02-29 09:53:00");
        t1.setParams(param);

        TestOrder t2 = new TestOrder();
        t2.setCreateTime("2024-02-29 09:53:00");
        t2.setParams(param);

        TestOrder t3 = new TestOrder();
        t3.setCreateTime("2024-02-29 09:53:00");
        t3.setParams(param);

        System.out.println("start test");

        new Thread(() -> {

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            long begin = System.currentTimeMillis();
            testOrderDao.save(t1);
            long cost1 = System.currentTimeMillis() - begin;
            System.out.println("cost1:" + cost1);

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            long begin2 = System.currentTimeMillis();
            testOrderDao.save(t2);
            long cost2 = System.currentTimeMillis() - begin2;
            System.out.println("cost2:" + cost2);

        }).start();

        new Thread(() -> {
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            long begin3 = System.currentTimeMillis();
            testOrderDao.save(t3);
            long cost3 = System.currentTimeMillis() - begin3;
            System.out.println("cost3:" + cost3);

        }).start();


        while (true) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

我的测试结果:

java mysql spring-boot hibernate spring-data-jpa
1个回答
0
投票

您可以在删除 log4j 和 slf4j 记录器后尝试此操作。

© www.soinside.com 2019 - 2024. All rights reserved.